Аутентификация с использованием request.POST, который хранится в SESSION, не работает
Я реализовал функцию входа в систему таким образом в django и хочу сохранить POST запрос после входа. Поэтому я сохранил request.POST в переменной SESSION, и попытался аутентифицироваться с помощью встроенной функции is_authenticated.
Но я получаю следующую ошибку:
'dict' object has no attribute 'user'
Когда я использую оригинальный request.POST, ошибка исчезает, но и информация для входа тоже. Вот почему я хранил логин POST в SESSION в первую очередь.
1- параметры входа обрабатываются в views.py:
def main_v(request):
template = loader.get_template('service_booking/index.html')
template_not_logged = loader.get_template('index_not_logged.html')
#get POST data from session
if 'login_request' in request.session:
login_session = request.session['login_request']
else:
login_session = request
#check if logged in or not by processing the SESSION variable 'login_session'
if login_session.user.is_authenticated:
print("authinticated")
print("user is:"+str(request.user))
return HttpResponse(template.render({},login_session.POST))
else:
print("not authinticated")
print("user is:"+str(request.user))
# create a form instance and populate it with data from the request:
form = login_form(request.POST)
return HttpResponse(template_not_logged.render({'form':form},request))
2- переменная сессии состоит в другой функции, которая обрабатывает форму входа:
def login_form_fun(request):
# if this is a POST request we need to process the form data
if request.method == 'POST':
# create a form instance and populate it with data from the request:
form = login_form(request.POST)
# check whether it's valid:
print("is is_valid")
if form.is_valid():
#fetch form data
uname = form.cleaned_data['uname']
pwd = form.cleaned_data['pwd']
#authinticate
auth_flag = login_user(request) #processing the authintication in another function that I didn't post here.
if auth_flag == True:
#if authintication is True
print("Compose session")
request.session['login_request'] = request.POST
return HttpResponseRedirect('index')
else:
print("ELSE")
#else authintication is False
return HttpResponseRedirect(reverse('index_not_logged'))
#if a GET (or any other method) we'll create a blank form
else:
print(request.method)
form = login_form()
return HttpResponseRedirect(reverse('index_not_logged'))