使用 UserCreationForm Django 从上一页处理过的电子邮件创建用户
Create a user with email which has been processed from the previous page with UserCreationForm Django
我正在尝试创建一个使用电子邮件作为用户名的用户,并且首先筛选该电子邮件,即该电子邮件未注册,然后创建一个用户。我如何传递或设置 forms.py 中的电子邮件,因为它已在上一页中处理过?
Models.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'
...
Forms.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>
邮件来自上一页保存的会话['email']。
我如何才能将此会话['email']传递给forms.py?
你可以这样试试:
# Form
class RegistrationForm(UserCreationForm):
class Meta:
model = CUser
fields = ('first_name', 'last_name', 'password1', 'password2')
def save(self, **kwargs):
email = kwargs.pop('email')
user = super(RegistrationForm, self).save(commit=False)
user.set_password(self.cleaned_data['password1'])
user.email = email
user.save()
return user
# View
# your form codes...
if form.is_valid():
form.save(email=request.session.get('email'))
# rest of the codes
我在这里做的是,首先我重写了保存方法以从关键字参数中捕获电子邮件值。然后我将电子邮件值传递给 ModelForm
的保存方法。希望对你有帮助!!
我正在尝试创建一个使用电子邮件作为用户名的用户,并且首先筛选该电子邮件,即该电子邮件未注册,然后创建一个用户。我如何传递或设置 forms.py 中的电子邮件,因为它已在上一页中处理过?
Models.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'
...
Forms.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>
邮件来自上一页保存的会话['email']。
我如何才能将此会话['email']传递给forms.py?
你可以这样试试:
# Form
class RegistrationForm(UserCreationForm):
class Meta:
model = CUser
fields = ('first_name', 'last_name', 'password1', 'password2')
def save(self, **kwargs):
email = kwargs.pop('email')
user = super(RegistrationForm, self).save(commit=False)
user.set_password(self.cleaned_data['password1'])
user.email = email
user.save()
return user
# View
# your form codes...
if form.is_valid():
form.save(email=request.session.get('email'))
# rest of the codes
我在这里做的是,首先我重写了保存方法以从关键字参数中捕获电子邮件值。然后我将电子邮件值传递给 ModelForm
的保存方法。希望对你有帮助!!