我想覆盖Django-login来创建自定义登录,但我找不到方法。
原因是在特定情况下,我不能使用csrf身份验证,所以我想创建一个自定义登录,然后创建一个安全层,以确保我的自定义登录是安全的。
有什么想法吗?
发布于 2014-05-09 23:17:53
答案就在这里:
https://docs.djangoproject.com/en/1.5/topics/auth/default/#how-to-log-a-user-in
代码类似于:
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
login(request, user)发布于 2014-05-10 04:51:35
要覆盖django自定义管理员,您必须创建urls路径和一个视图,您可以在其中检查和登录/注销用户。以此为例:
urls.py
url(r'^accounts/auth/$', 'auth_view'),views.py
from django.contrib import auth
def auth_view(request):
# here you get the post request username and password
username = request.POST.get('username', '')
password = request.POST.get('password', '')
# authentication of the user, to check if it's active or None
user = auth.authenticate(username=username, password=password)
if user is not None:
if user.is_active:
# this is where the user login actually happens, before this the user
# is not logged in.
auth.login(request, user)
...
return ...
else :
return HttpResponseRedirect("Invalid username or password")您的html表单:
<form role="form" action="/accounts/auth/" method="POST">发布于 2017-12-13 15:00:58
在urls.py中
url(r'^$', auth_views.login, {'template_name': 'home/login.html'}, name='login')在login.html中
<form method="post">
{{ form.as_p }}
{{ form.non_field_errors }}
<input type="submit">
</form>https://stackoverflow.com/questions/23567373
复制相似问题