Python 如果重定向然后呈现模板,如何传递请求
我有一个要求,从登录页面跳转到管理页面,您知道URL地址应该更改为管理页面 如果我只使用render to admin页面,URL地址将不会更改,因此在这里我得到了OptimuScime的好答案 但如果我重定向然后呈现模板,我就不能将请求从登录页面传递到管理页面 在登录页面的views.py中:Python 如果重定向然后呈现模板,如何传递请求,python,django,Python,Django,我有一个要求,从登录页面跳转到管理页面,您知道URL地址应该更改为管理页面 如果我只使用render to admin页面,URL地址将不会更改,因此在这里我得到了OptimuScime的好答案 但如果我重定向然后呈现模板,我就不能将请求从登录页面传递到管理页面 在登录页面的views.py中: ... return redirect('/app_admin/index/') ... return render(request, 'app_admin/index.html') # the
...
return redirect('/app_admin/index/')
...
return render(request, 'app_admin/index.html') # there the request is None.
在管理页面的views.py中:
...
return redirect('/app_admin/index/')
...
return render(request, 'app_admin/index.html') # there the request is None.
如何将请求传递给管理员页面的views.py?您应该看看一些基本的Django教程,例如,其中描述了如何创建登录处理程序 要点是: 在用户提交表单的视图中,计算用户名和/或密码。如果提交了正确的信息(用户名和密码),则将此信息保存在会话中。将用户重定向到登录(限制)区域并检查会话。如果会话具有正确的信息,则允许用户查看内容,否则将用户重定向到外部 简单登录逻辑(说明性):
def login(request):
m = Member.objects.get(username=request.POST['username'])
if m.password == request.POST['password']:
# Username and password is correct, save that the user is logged in in the session variable
request.session['logged_in'] = True
request.session['username'] = request.POST['password']
# Redirect the user to the admin page
return redirect('/app_admin/index/')
else:
# Username and/or password was incorrect
return HttpResponse("Your username and password didn't match.")
def admin_index(request):
# Make sure that the user is logged in
if 'logged_in' in request.session and request.session['logged_in']:
# User is logged in, display the admin page
return render(
request,
'app_admin/index.html',
{'username': request.session['username']}
) # You can now use {{ username }} in your view
# User is not logged in and should not be here. Display error message or redirect the user to the login page
return HttpResponse("You are not logged in")
简单的管理页面逻辑(说明性):
def login(request):
m = Member.objects.get(username=request.POST['username'])
if m.password == request.POST['password']:
# Username and password is correct, save that the user is logged in in the session variable
request.session['logged_in'] = True
request.session['username'] = request.POST['password']
# Redirect the user to the admin page
return redirect('/app_admin/index/')
else:
# Username and/or password was incorrect
return HttpResponse("Your username and password didn't match.")
def admin_index(request):
# Make sure that the user is logged in
if 'logged_in' in request.session and request.session['logged_in']:
# User is logged in, display the admin page
return render(
request,
'app_admin/index.html',
{'username': request.session['username']}
) # You can now use {{ username }} in your view
# User is not logged in and should not be here. Display error message or redirect the user to the login page
return HttpResponse("You are not logged in")
请注意,这是两个不同的视图(和URL),您必须在中映射。如果我想在
/app\u admin/index/
中使用密码,我是否应该在会话中存储密码?或者最好使用/login/
中的密码。除了登录用户之外,您不应该使用用户密码。但是,您可以在会话变量中保存所需的任何信息。请参阅我的更新答案,其中我保存了当前用户名。我使用密码连接其他应用程序,因此我在/login/中进行连接,非常感谢。您也可以发送密码,这只是安全风险问题以及“好”和“坏”的问题。当然可以。希望这解决了你的问题。你真的应该考虑学习HTTP协议是如何工作的…