0.0.16
login routines
This commit is contained in:
@@ -12,6 +12,69 @@ from datetime import datetime
|
||||
from django.template.loader import render_to_string
|
||||
from django.urls import reverse
|
||||
|
||||
|
||||
|
||||
def new_route_view_ajax(request):
|
||||
if request.method != 'POST':
|
||||
raise Http404
|
||||
|
||||
from RoutesApp.forms import RegistrationForm
|
||||
Dict = {
|
||||
'form': RegistrationForm()
|
||||
}
|
||||
|
||||
html = render_to_string('blocks/profile/b_new_route.html', Dict, request=request)
|
||||
return JsonResponse({'html': html}, status=200)
|
||||
|
||||
|
||||
def login_ajax(request):
|
||||
if request.method != 'POST':
|
||||
raise Http404
|
||||
|
||||
try:
|
||||
|
||||
data = request.POST
|
||||
|
||||
from .forms import LoginForm
|
||||
form = LoginForm(data)
|
||||
if not form.is_valid():
|
||||
Dict = {'form': form}
|
||||
html = render_to_string('forms/f_registration.html', Dict, request=request)
|
||||
return JsonResponse({'html': html}, status=400)
|
||||
|
||||
from django.contrib.auth import authenticate
|
||||
user = authenticate(username=form.data['username'], password=form.data['password'])
|
||||
if user is not None:
|
||||
auth.login(request, user)
|
||||
else:
|
||||
errors_Dict = {
|
||||
'errors': {
|
||||
'__all__': f'неверный логин и\или пароль'
|
||||
}
|
||||
}
|
||||
Dict = {'form': errors_Dict}
|
||||
html = render_to_string('forms/f_login.html', Dict)
|
||||
return JsonResponse({'html': html}, status=400)
|
||||
|
||||
|
||||
res_Dict = {
|
||||
'redirect_url': reverse('user_profile')
|
||||
}
|
||||
|
||||
return JsonResponse(res_Dict)
|
||||
|
||||
except Exception as e:
|
||||
|
||||
errors_Dict = {
|
||||
'errors': {
|
||||
'__all__': f'ошибка в запросе = {str(e)}'
|
||||
}
|
||||
}
|
||||
Dict = {'form': errors_Dict}
|
||||
html = render_to_string('forms/f_login.html', Dict)
|
||||
return JsonResponse({'html': html}, status=400)
|
||||
|
||||
|
||||
def registration_ajax(request):
|
||||
if request.method != 'POST':
|
||||
raise Http404
|
||||
|
||||
Reference in New Issue
Block a user