编辑:当我输入正确的用户名和密码时,视图现在会登录,但是当我输入错误的用户名和密码时,不会返回任何错误 . 我正在使用Django提供的通用登录视图 . 这是我的login.html模板:
<body>
<h1>User Login</h1>
{% if form.has_errors %}
<p>Your username and password did not match.
Please try again.</p>
{% else %}
<form method="post" action=" . ">{% csrf_token %}
<p><label for="id_username">Username:</label>
{{ form.username }}</p>
<p><label for="id_password">Password:</label>
{{ form.password }}</p>
<input type="hidden" name="next" value="/" />
<input type="submit" value="login" />
</form>
{% endif %}
</body>
现在,我正在阅读“Pact Publishing . 使用Django学习网站开发”一书中的教程 . 视图永远不会返回
form.has_errors
即使我故意提交错误的用户名和密码 . 点击提交后,它一直让我回到同一页面 . 另外,根据我读过的HTML书,它说表单中的'action'属性是服务器上将检索信息的页面的URL . 这就是为什么它不起作用?因为我的
action=" . "
?
编辑:视图是通用登录视图:
@sensitive_post_parameters()
@csrf_protect
@never_cache
def login(request, template_name='registration/login.html',
redirect_field_name=REDIRECT_FIELD_NAME,
authentication_form=AuthenticationForm,
current_app=None, extra_context=None):
"""
Displays the login form and handles the login action.
"""
redirect_to = request.REQUEST.get(redirect_field_name, '')
if request.method == "POST":
form = authentication_form(data=request.POST)
if form.is_valid():
# Ensure the user-originating redirection url is safe.
if not is_safe_url(url=redirect_to, host=request.get_host()):
redirect_to = resolve_url(settings.LOGIN_REDIRECT_URL)
# Okay, security check complete. Log the user in.
auth_login(request, form.get_user())
if request.session.test_cookie_worked():
request.session.delete_test_cookie()
return HttpResponseRedirect(redirect_to)
else:
form = authentication_form(request)
request.session.set_test_cookie()
current_site = get_current_site(request)
context = {
'form': form,
redirect_field_name: redirect_to,
'site': current_site,
'site_name': current_site.name,
}
if extra_context is not None:
context.update(extra_context)
return TemplateResponse(request, template_name, context,
current_app=current_app)
而我的urls.py就是
url(r'^$', main_page),
url(r'^user/(\w+)/$', user_page),
url(r'^login/$', login),
2 回答
试试这个 .
Login.Html
urls.py
settings.py
啊,在这里找到答案:form.has_errors tag not working
被替换为