Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django在注册时总是返回无效表单_Django_Django Forms_Django Views_Django 2.0_Saleor - Fatal编程技术网

Django在注册时总是返回无效表单

Django在注册时总是返回无效表单,django,django-forms,django-views,django-2.0,saleor,Django,Django Forms,Django Views,Django 2.0,Saleor,我正在尝试在注册期间实现电子邮件身份验证。但是编译器总是返回无效的表单。有人能提出这背后的原因吗?我正在使用SALEOR软件包进行此项目。下面我发布了模型、表单和视图代码 models.py class User(PermissionsMixin, AbstractBaseUser): email = models.EmailField(unique=True) addresses = models.ManyToManyField(Address, blank=True)

我正在尝试在注册期间实现电子邮件身份验证。但是编译器总是返回无效的表单。有人能提出这背后的原因吗?我正在使用SALEOR软件包进行此项目。下面我发布了模型、表单和视图代码

models.py

class User(PermissionsMixin, AbstractBaseUser):
    email = models.EmailField(unique=True)
    addresses = models.ManyToManyField(Address, blank=True)
    is_staff = models.BooleanField(default=False)
    is_active = models.BooleanField(default=True)
    date_joined = models.DateTimeField(default=timezone.now, editable=False)
    default_shipping_address = models.ForeignKey(
        Address, related_name='+', null=True, blank=True,
        on_delete=models.SET_NULL)
    default_billing_address = models.ForeignKey(
        Address, related_name='+', null=True, blank=True,
        on_delete=models.SET_NULL)

    USERNAME_FIELD = 'email'

    objects = UserManager()
forms.py

class SignupForm(forms.ModelForm):
    password = forms.CharField(
        widget=forms.PasswordInput)
    email = forms.EmailField(
        error_messages={
            'unique': pgettext_lazy(
                'Registration error',
                'This email has already been registered.')})

    class Meta:
        model = User
        fields = ('email',)
        labels = {
            'email': pgettext_lazy(
                'Email', 'Email'),
            'password': pgettext_lazy(
                'Password', 'Password')}

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        if self._meta.model.USERNAME_FIELD in self.fields:
            self.fields[self._meta.model.USERNAME_FIELD].widget.attrs.update(
                {'autofocus': ''})

    def save(self, request=None, commit=True):
        user = super().save(commit=False)
        password = self.cleaned_data['password']
        user.set_password(password)
        if commit:
            user.save()
        return user
views.py 每次执行if form.u的else部分都有效

def signup(request):

if request.method == 'POST':
    form = SignupForm(request.POST or None)
    print('POST Method')
    if form.is_valid():
        print('Valid Form')

        user = form.save(commit=False)
        user.is_active = False
        user.save()

        current_site = get_current_site(request)
        mail_subject = "Activate Your Account"
        message = render_to_string('account/activate.html', {
            'user': user,
            'domain': current_site.domain,
            'uid':force_text(urlsafe_base64_encode(user.pk)),

            'token':account_activation_token.make_token(user),
        })
        to_email = form.cleaned_data.get('email')
        from_email = settings.EMAIL_HOST_USER
        email = EmailMessage(
                    mail_subject, message,  to=[to_email]
        )
        email.send()
        print('Mail Sent')
        return HttpResponse('Please confirm your email address to complete the registration')
    else:
        form = SignupForm()
        print('else of form valid')
        return render(request, 'account/signup.html', {'form': form})
else:
    print('Not POST Method')
    form = SignupForm()
    return render(request,'account/signup.html',{'form':form})

form=SignupForm(request.POST或None)
为什么要在括号中添加
或None
?您应该使用表单
form=SignupForm(request.POST)
绑定
request.POST
,我正在使用SALEOR包,它从一开始就存在。我尝试过删除None但没有结果@HaiFzhan如果注册表单绑定None没有意义,字段是必需的,对吗?是的,所有字段都是必需的@HaiFzhan如果你总是得到
无效表单
,那是因为表单实际上是无效的。将
print(“form.errors”)
添加到此行之后
print('else of form valid')
{{form.errors}}
以在模板中进行调试。顺便说一句,在最后7行中,您不需要再次分配表单。移除它们。