'User'对象没有'ValidationError'属性

时间:2018-04-09 05:35:32

标签: python django django-forms

我正在尝试创建注册表单。一切正常,直到我尝试在电子邮件已存在的情况下提出验证错误。它说,'User' object has no attribute 'ValidationError'

这就是我做的,

def register(request):
    if request.method == 'POST':
        form = RegistrationForm(request.POST)
        if form.is_valid():
            new_form = form.save(commit=False)
            mail = form.cleaned_data['email']
            if User.objects.filter(email=mail).exists():
                raise new_form.ValidationError('Looks like email already exists')
            else:
                new_form.save()
            username = request.POST.get('username')
            password = request.POST.get('password1')
            user = authenticate(username=username, password=password)
            login(request, user)
            return redirect(reverse('accounts:profile'))
    else:
        form = RegistrationForm()

我们如何解决这个问题?

5 个答案:

答案 0 :(得分:0)

尝试在表单中添加以下方法

from django import forms
from .models import User

class RegistrationForm(forms.Form):

   # ..............
   # ..............

   def clean(self):
      mail = form.cleaned_data['email']
      if User.objects.filter(email=mail).exists():
          raise forms.ValidationError('Looks like email already exists')
      return self.cleaned_data

更改views.py

中的代码
if User.objects.filter(email=mail).exists():
            raise new_form.ValidationError('Looks like email already exists')
        else:
            new_form.save()

new_form.save()

答案 1 :(得分:0)

验证表单错误时,应从django.forms导入validationError而不是django.core.exceptions

尝试:

from django.forms import ValidationError

答案 2 :(得分:0)

你应该像这样引发ValidationError:

raise ValidationError('Looks like email already exists')

以下是完整代码:

def register(request):
    if request.method == 'POST':
        form = RegistrationForm(request.POST)
            if form.is_valid():
                new_form = form.save(commit=False)
                mail = form.cleaned_data['email']
            if User.objects.filter(email=mail).exists():
                raise ValidationError('Looks like email already exists')
            else:
                new_form.save()
            username = request.POST.get('username')
            password = request.POST.get('password1')
            user = authenticate(username=username, password=password)
            login(request, user)
            return redirect(reverse('accounts:profile'))
    else:
        form = RegistrationForm()

答案 3 :(得分:0)

if User.objects.filter(email=mail).exists():
            raise new_form.ValidationError('Looks like email already exists')

使用“ forms”而不是“ new_form”,结果应类似于

if User.objects.filter(email=mail).exists():
                raise forms.ValidationError('Looks like email already exists')

答案 4 :(得分:0)

我输入如下内容,它在django-2.0.7上有效:

from django import forms
from django.contrib.auth import login
from django.contrib.auth.models import User
from django.shortcuts import render, redirect


class RegistrationForm(forms.ModelForm):
    def clean_email(self):
        email = self.cleaned_data['email']
        if self.Meta.model.objects.filter(email=email).exists():
            raise forms.ValidationError('Looks like email already exists')
        return email

    class Meta:
        model = User
        fields = ('username', 'password', 'email')


def register(request):
    if request.method == 'POST':
        form = RegistrationForm(request.POST)
        if form.is_valid():
            user = form.save(commit=False)
            user.set_password(user.password)
            user.save()
            login(request, user)
            return redirect('accounts:profile')
    else:
        form = RegistrationForm()
    return render(request, 'register.html', {'form': form})