我使用了基于本教程的扩展用户模型。当我提交注册表时,会发生此错误。我在代码中使用 is_active() 和 is_active 时都会显示此错误。
如何解决此错误?
这是我的代码:
模型.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)
相关分类