我遇到问题,确保我的用户的全名在注册时保存在用户记录中。我正在使用Django 1.5和django-registration 1.0,虽然我之前使用django和django-registration的早期版本完成了这个,但是我很难用基于类的视图和信号来处理这个问题。
用户注册创建了一切正常,所以我很确定我的信号是错误的。
以下是我的代码连接的地方backends.py
:
from django.contrib.sites.models import Site
from app.forms import MyRegistrationForm
from registration import signals
from registration.backends.default.views import RegistrationView
from registration.signals import user_activated
class MyRegistrationView(RegistrationView):
form_class = MyRegistrationForm
def register(self, request, **cleaned_data):
username, email, password, first_name, last_name = cleaned_data['username'], cleaned_data['email'], cleaned_data['password1'], cleaned_data['first_name'], cleaned_data['last_name']
if Site._meta.installed:
site = Site.objects.get_current()
else:
site = RequestSite(request)
new_user = RegistrationProfile.objects.create_inactive_user(
username, email, password, site)
signals.user_registered.send(sender=self.__class__,
user=new_user,
request=request,
first_name=first_name,
last_name=last_name)
return new_user
def create(sender, user, request, **kwargs):
""" Saves the user's full name to the user record. """
user.first_name = first_name
user.last_name = last_name
user.save()
user_activated.connect(create)
如果我确实收到了正确的信号并且我的表单存在问题,那么这里是forms.py
:
attrs_dict = {'class': 'required'}
logger = logging.getLogger(__name__)
class MyRegistrationForm(RegistrationFormTermsOfService):
"""
Custom registration form performs unique email checking to prevent users with the same email address from registering more than once.
"""
def __init__(self, *args, **kwargs):
super(MyRegistrationForm, self).__init__(*args, **kwargs)
# adding custom error messages
self.fields['username'].error_messages = {'required': 'Please enter a username.'}
self.fields['email'].error_messages = {'required': 'Please enter a valid email address.'}
self.fields['first_name'].error_messages = {'required': 'Please enter a first name.'}
self.fields['last_name'].error_messages = {'required': 'Please enter a last name.'}
self.fields['password1'].error_messages = {'required': 'Please enter a password.'}
self.fields['password2'].error_messages = {'required': 'Please confirm your password.'}
logger.debug('Registration form loaded')
def clean_email(self):
if User.objects.filter(email__iexact=self.cleaned_data['email']):
raise forms.ValidationError(_("This email address is already in use. Please supply a different email address."))
return self.cleaned_data['email']
first_name = forms.CharField(label="First Name", widget=forms.TextInput(attrs=attrs_dict))
last_name = forms.CharField(label="Last Name", widget=forms.TextInput(attrs=attrs_dict))
答案 0 :(得分:0)
我相信这件事在你的form.py中。如果你可以直接使用模型鬃毛用Meta类替换整个__init__()
类,那会更好。
答案 1 :(得分:0)
实际用户由registration / models.py。中的create_inactive_user函数创建。
您可能想尝试添加字段,以便模型函数看起来像:
def create_inactive_user(self, username, email, password, first_name, last_name,
site, send_email=True):
new_user = User.objects.create_user(username, email, password,
first_name=first_name, last_name=last_name)
new_user.is_active = False
new_user.save()
registration_profile = self.create_profile(new_user)
if send_email:
registration_profile.send_activation_email(site)
return new_user
不保证这会起作用!祝你好运!