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

时间:2018-04-26 04:54:33

标签: 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)
    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.is_valid的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})

0 个答案:

没有答案