Django- ValueError [由于数据未验证而无法创建用户]

时间:2018-11-21 08:04:17

标签: python django

我使用了基于this教程的扩展用户模型。当我提交注册表单时,会发生此错误。该错误同时显示我在代码中同时使用is_active()和is_active。
如何解决此错误?
Error
registration form
new error
这是我的代码:

models.py

from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver

class Profile(models.Model):
    user = models.OneToOneField(User,on_delete=models.CASCADE)
    full_name = models.CharField(max_length=256, blank=False)
    codeforces_id = models.CharField(max_length=256, blank=False)
    Uva_Id = models.CharField(max_length=256, blank=False)
    #receiver(post_save, sender=User)
    def create_user_profile(sender, instance, created, **kwargs):
        if created:
            Profile.objects.create(user=instance)

    #receiver(post_save, sender=User)
    def save_user_profile(sender, instance, **kwargs):
        instance.profile.save()

views.py

from django.http import HttpResponse 
from django.shortcuts import render,redirect
from django.contrib.auth import authenticate,login
from django.views import generic
from django.views.generic import View
from .forms import UserForm,ProfileForm

class UserFormView(View):
    user_form_class = UserForm
    profile_form_class= ProfileForm
    #display a blank form
    def get(self , request):
         user_form = self.user_form_class(None)
         profile_form = self.profile_form_class(None)
         return render(request, 'website/registration_form.html',{
            'user_form':user_form,
            'profile_form':profile_form
            })
    #process form data
    def post(self, request):
        user_form = self.user_form_class(request.POST)
        profile_form = self.profile_form_class(request.POST)
        user= user_form.save(commit=  False)
        if user_form.is_valid() and profile_form.is_valid():
            password= user_form.cleaned_data['password']
            username = user_form.cleaned_data['username']
            user.set_password(password)

            user_form.save()
            profile_form.save()

            # auto login 
            user = authenticate(username =username, password = password)

            if user is not None:
                if user.is_active():
                    login(request,user)
                    return redirect('website:index')
        return render(request, 'website/registration_form.html',{
            'user_form':user_form,
            'profile_form':profile_form
            })

1 个答案:

答案 0 :(得分:0)

您在验证之前保存了表单。 将user = user_form.save(commit= False)添加到您的if语句中

def post(self, request):
        user_form = UserForm(request.POST, instance=request.user)
        profile_form = ProfileForm(request.POST, instance=request.user.profile)

        if user_form.is_valid() and profile_form.is_valid():
            user= user_form.save(commit=  False)
            password= user_form.cleaned_data['password']
            username = user_form.cleaned_data['username']
            user.set_password(password)

            user.save() # You can update user object like this
            profile_form.save()

            # auto login 
            user = authenticate(username =username, password = password)

            if user is not None:
                if user.is_active():
                    login(request,user)
                    return redirect('website:index')
        return render(request, 'website/registration_form.html',{
            'user_form':user_form,
            'profile_form':profile_form
            })

第二个错误是由于调用user.save()时,它将自动执行模型中定义的信号。但是您评论了@receiver(post_save, sender=User)。这很关键,请不要评论。