我正在使用 django registratations.backends.simple.urls,我想在我的注册页面中添加一个自定义字段
simple.backend 与 default.backend 不同,因为它是轻量级的并且不需要用户验证配置(“smtp、电子邮件等”)
模型.py
from registration.signals import user_registered
from django.dispatch import receiver
class UserProfile(models.Model):
user = models.OneToOneField(User, unique=True, primary_key=True, related_name="user")
# Extra attributes
bio = models.TextField(null=True)
country = models.ForeignKey(Countries, null=True)
@receiver(user_registered)
def registration_active_country(sender, user, request, **kwargs):
print >> sys.stderr , request.POST['country']
funid = request.POST['country']
a = Countries.objects.get(pk=funid)
userid = user.id
user = UserProfile.objects.get(pk=userid)
user.country = a
user.save()
网址.py
url(r'^accounts/register/$', register, {'backend': 'registration.backends.simple.SimpleBackend','form_class': UserRegistrationForm}, name='registration_register'),
url(r'^accounts/', include("registration.backends.simple.urls")),
表格.py
class UserRegistrationForm(RegistrationForm):
country = forms.ModelChoiceField(queryset=Countries.objects, label=u'Country', empty_label=u'Not defined')
有更好的答案吗?