Django-ValueError [无法创建用户,因为数据没有验证]

我使用了基于教程的扩展用户模型。当我提交注册表时,会发生此错误。我在代码中使用 is_active() 和 is_active 时都会显示此错误。
如何解决此错误?

http://img4.mukewang.com/612498f00001bcdb13570651.jpg

http://img1.mukewang.com/612498f8000136f107770260.jpg

http://img.mukewang.com/6124990100011ee913630662.jpg

这是我的代码:


模型.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()

视图.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)


慕后森
浏览 140回答 1
1回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python