猿问

使用 UserCreationForm Django 从上一页处理的电子邮件创建用户

我正在尝试使用电子邮件作为用户名创建用户,并且首先筛选电子邮件,即电子邮件未注册然后创建用户。我如何在 forms.py 中传递或设置电子邮件,因为它已经在上一页中处理过了?


模型.py


from django.contrib.auth.models import AbstractBaseUser

from django.db import models


class CUserManager(models.Manager):

    def _create_user(self, email, password, **extra_fields):


        now = timezone.now();


        if not email:

            raise ValueError(_('Email is required'))


        user = self.model(

            email = email,

            date_joined = now, **extra_fields

        )


        user.set_password(password)  

        user.save(using = self._db)


        return account


    def create_user(self, email, password=None, **extra_fields):

        return self._create_user(email, password, **extra_fields)


    def get_by_natural_key(self, email):

        return self.get(email=email)


    def create_superuser(self, email, password, **extra_fields):

        return self._create_user(email, password,**extra_fields)


class CUser(AbstractBaseUser):


    email = models.EmailField(_('email address'), unique=True)

    first_name = models.CharField(_('first name'), max_length=255)

    last_name = models.CharField(_('last name'), max_length=255)

    date_joined = models.DateTimeField(_('date created'), auto_now_add=True)

    is_active = models.BooleanField(default=True)    


    objects = CUserManager()


    USERNAME_FIELD = 'email'

...

表单.py


from django.contrib.auth.forms import UserCreationForm


class RegistrationForm(UserCreationForm):


    class Meta: 

        model = CUser

        fields = ('first_name', 'last_name', 'password1', 'password2')

在 HTML 中


 <form action=""  method="post" role="form">

    {% csrf_token %}

  {{ form.as_p }}

  <input type="submit" value="Submit" />

</form>

电子邮件来自保存在上一页中的 session['email']。


如何将此会话 ['email'] 传递给 forms.py?


慕森卡
浏览 172回答 1
1回答
随时随地看视频慕课网APP

相关分类

Python
我要回答