如何让Django视图返回表单错误

时间:2014-01-14 23:09:53

标签: django forms django-views django-authentication django-registration

这是我的观点:

def main_page(request):

    if request.method == 'POST':
        form = RegistrationForm(request.POST)
        if form.is_valid():
            user = User.objects.create_user(
                username=form.clean_data['username'],
                password=form.clean_data['password1'],
                email=form.clean_data['email']
            )
        return HttpResponseRedirect('/')
    else:
        form = RegistrationForm()
        variables = {
            'form': form
        }
        return render(request, 'main_page.html', variables)

这是我的main_page.html:

{% if form.errors %}
    <p>NOT VALID</p>
    {% for errors in form.errors %}
        {{ errors }}
    {% endfor %}
{% endif %}
<form method="post" action="/">{% csrf_token %}
    <p><label for="id_username">Username:</label>{{ form.username }}</p>
    <p><label for="id_email">Email Address:</label>{{ form.email }}</p>
    <p><label for="id_password">Password:</label>{{ form.password1 }}</p>
    <p><label for="id_retypePassword">Retype Password:</label>{{ form.password2 }}</p>
    <input type="hidden" name="next" />
    <input type="submit" value="Register" />
</form>

当我转到使用main_page视图的url时,它只显示表单。当我提交包含错误的表单(包含空白字段且没有正确的电子邮件地址)时,它只会将我重定向到同一页面,并且不会显示任何错误。它甚至没有说“无效”。

当我改变

{% if form.errors %}

{% if not form.is_valid %}

它总是说“无效”(即使它是第一次进入网址,即使我还没有提交任何内容)。

这是我的RegistrationForm:

from django import forms
import re
from django.contrib.auth.models import User
from django.core.exceptions import ObjectDoesNotExist

class RegistrationForm(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())

    def clean_password2(self):
        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.')

        def clean_username(self):
        username = self.cleaned_data['username']
        if not re.search(r'^\w+$', username): #checks if all the characters in username are in the regex. If they aren't, it returns None
            raise forms.ValidationError('Username can only contain alphanumeric characters and the underscore.')
        try:
            User.objects.get(username=username) #this raises an ObjectDoesNotExist exception if it doesn't find a user with that username
        except ObjectDoesNotExist:
            return username #if username doesn't exist, this is good. We can create the username
        raise forms.ValidationError('Username is already taken.')

3 个答案:

答案 0 :(得分:9)

它正在重定向您,因为如果方法是POST,您总是返回HttpResponseRedirect,即使表单不是vaild。试试这个:

def main_page(request):
    form = RegistrationForm()
    if request.method == 'POST':
        form = RegistrationForm(request.POST)
        if form.is_valid():
            user = User.objects.create_user(
                username=form.clean_data['username'],
                password=form.clean_data['password1'],
                email=form.clean_data['email']
            )
            return HttpResponseRedirect('/')        
    variables = {
        'form': form
    }
    return render(request, 'main_page.html', variables)

这样,调用is_valid的表单实例就会传递给模板,并且有机会显示错误。仅当表单有效时,才会重定向用户。如果您想要引人入胜,请在重定向之前使用messages framework添加消息。

如果你想要它更简洁一点:

def main_page(request):
    form = RegistrationForm(request.POST or None)
    if form.is_valid():
        user = User.objects.create_user(
            username=form.clean_data['username'],
            password=form.clean_data['password1'],
            email=form.clean_data['email']
        )
        return HttpResponseRedirect('/')        
    variables = {
        'form': form
    }
    return render(request, 'main_page.html', variables)

答案 1 :(得分:3)

让您的观点如下:

if form.is_valid():
    pass
    # actions
else:
    # form instance will have errors so we pass it into template
    return render(request, 'template.html', {'form': form})

在模板中,您可以遍历form.errors或简单:

{{ forms.as_p }}

答案 2 :(得分:1)

您可以重新设置视图的格式,以在控制台中显示表单错误,如下所示:

def main_page(request):

    if request.method == 'POST':
        form = RegistrationForm(request.POST)
        if form.is_valid():
            user = User.objects.create_user(
                username=form.clean_data['username'],
                password=form.clean_data['password1'],
                email=form.clean_data['email']
            )
        else:
            print(form.errors)
            return HttpResponse("Form Validation Error")

        return HttpResponseRedirect('/')
    else:
        form = RegistrationForm()
        variables = {
            'form': form
        }
        return render(request, 'main_page.html', variables)


与往常一样,保持上面的缩进。如果表单有任何错误,它将显示在控制台中,如

<ul class="errorlist"><li>date<ul class="errorlist"><li>Enter a valid date.</li></ul></li></ul>

希望对您有帮助