【发布时间】:2016-07-12 11:55:19
【问题描述】:
我正在使用Learning Website Development with Django 一书。
我已经从 github 的休闲路径 Source code 下载了源代码。我能够在我的 Windows 机器上运行该应用程序。
但是由于某种原因,无论何时登录或注册在提交后包含错误,这些错误不会显示在 html 页面中。
请让我知道是什么问题。我在浏览器控制台中看到的一个观察是
以下代码行显示错误信息
def register_page(request):
form = RegistrationForm(request.POST or None)
print request.method
if request.method == 'POST' and form.is_valid():
user = User.objects.create_user(
username=form.cleaned_data['username'],
password=form.cleaned_data['password1'],
email=form.cleaned_data['email']
)
return HttpResponseRedirect('/register/success/')
else:
form = RegistrationForm()
variables = RequestContext(request, {'form': form})
print variables
return render_to_response('registration/register.html', variables)
注册页面html代码:
{% extends "base.html" %}
{% block title %}User Registration{% endblock %}
{% block head %}User Registration{% endblock %}
{% block content %}
print "ddd"
{% if form.has_errors %}
<p>Your username and password didn't match.
Please try again.</p>
{% endif %}
<form method="post" action="">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="register" />
</form>
{% endblock %}
登录页面代码:
{% extends "base.html" %}
{% block title %}User Login{% endblock %}
{% block head %}User Login{% endblock %}
{% block content %}
{% if form.has_errors %}
<p>Your username and password didn't match.
Please try again.</p>
{% endif %}
<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="submit" value="login" />
<input type="hidden" name="next" value="/" />
</form>
{% endblock %}
form.py
import re
from django.core.exceptions import ObjectDoesNotExist
from django.contrib.auth.models import User
from django import forms
class RegistrationForm(forms.Form):
print forms.Form;
username = forms.CharField(
label='Username',
max_length=30
)
email = forms.EmailField(
label='Email'
)
password1 = forms.CharField(
label='Password',
widget=forms.PasswordInput()
)
password2 = forms.CharField(
label='Password (Again)',
widget=forms.PasswordInput()
)
# password validation:
def cleaned_password2(self):
# all valid values are accessible trough self.clean_data
if 'password1' in self.cleaned_data:
password1 = self.cleaned_data['password1']
password2 = self.cleaned_data['password2']
if password1 == password2:
return password2
raise forms.ValidationError('Passwords do not match.')
# username validation
def cleaned_username(self):
username = self.cleaned_data['username']
if not re.search(r'^\w+$', username):
raise forms.ValidationError('Username can only contain alphanumeric characters and the underscore.')
try:
User.objects.get(username=username)
except ObjectDoesNotExist:
return username
raise forms.ValidationError('Username is already taken.')
class BookmarkSaveForm(forms.Form):
url = forms.URLField(
label='URL',
widget=forms.TextInput(attrs={'size': 64})
)
title = forms.CharField(
label='Title',
widget=forms.TextInput(attrs={'size': 64})
)
tags = forms.CharField(
label='Tags',
widget=forms.TextInput(attrs={'size': 64})
)
http://localhost:8000/static/style.css资源加载失败:服务器响应状态为404(未找到)
【问题讨论】:
-
需要一段应该显示表单错误的代码。
-
我正在使用 python2.7 和 django1.9.x
标签: django python-2.7 django-forms