实际上,当我使用用户名注册时,它会重定向到注册完成页面。但是当我使用该用户名登录时,它会抛出表单错误。因此,当我使用我创建的用户名登录时,它会显示
“您的用户名和密码不匹配。请重试”
(我写的是在login.html中显示form.errors)
login.html
{% extends "base.html" %}
{% block content %}
{% if form.errors %}
<p>Your username and password didn't match. Please try again.</p>
{% endif %}
<form method="post" action="{% url 'django.contrib.auth.views.login' %}">
{% csrf_token %}
<table>
<tr>
<td>{{ form.username.label_tag }}</td>
<td>{{ form.username }}</td>
</tr>
<tr>
<td>{{ form.password.label_tag }}</td>
<td>{{ form.password }}</td>
</tr>
</table>
<input type="submit" value="login" />
<input type="hidden" name="next" value="{{ next }}" />
</form>
{% endblock %}
registration.html
<html>
<head>
<title>Register</title>
</head>
<body>
<h2>Register</h2>
<form action="/accounts/register/" method="POST">{% csrf_token %}
<table>
{{form.as_table}}
</table>
<p>
<input type="submit" value="Submit">
</p>
</form>
</body>
</html>
urls.py
enter code here
from django.conf.urls import patterns, include, url
from frm.views import main,forum,thread
from frm.views import reply,new_thread,profile,post
from django.conf import settings
from django.contrib import admin
from django.conf.urls import *
from django.contrib.auth.views import login
admin.autodiscover()
urlpatterns = patterns('',
url(r'^$',main),
url(r'^forum/(?P<pk>\w+)/$',forum ),
url(r'^thread/(?P<pk>\w+)/$',thread ),
url(r'^post/(new_thread|reply)/(\d+)',post),
url(r'^new_thread/(\d+)/$',new_thread),
url(r'^reply/(\d+)/$',reply),
url(r'^profile/(?P<pk>\w+)/$',profile),
# Examples:
# url(r'^$', 'forum.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^accounts/',include('registration.backends.default.urls')),
url(r'^admin/', include(admin.site.urls)),
)
答案 0 :(得分:0)
您可以通过多种方式访问模板中的错误。尝试更改:
{% if form.errors %}
<p>Your username and password didn't match. Please try again.</p>
{% endif %}
为:
{% if form.errors %}
{{ form.errors }}
<br /><br />
{% if form.non_field_errors %}
{% for error in form.non_field_errors %}
{{ error }}
{% endfor %}
<br /><br />
a nicer way:<br />
{{ form.non_field_errors.as_ul }}
{% endif %}
<br /><br />
form username errors:<br />
{{ form.username.errors }}
<br /><br />
form password errors:<br />
{{ form.password.errors }}
{% endif %}
这是一个好的开始。您还需要向访问者显示错误。将您的label_tag
更改为
<td>{{form.username.errors }}<br />{{ form.username.label_tag }}</td>
和
<td>{{form.password.errors }}<br />{{ form.password.label_tag }}</td>