使用simple.backend定制django-registration的表单字段

时间:2013-06-25 04:36:09

标签: django django-registration

我正在使用django registratations.backends.simple.urls,我想在我的注册页面添加自定义字段

simple.backend与default.backend不同,因为它很轻,不需要用户验证配置(“smtp,email等”)

models.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()

urls.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")),

forms.py

class UserRegistrationForm(RegistrationForm):
    country = forms.ModelChoiceField(queryset=Countries.objects, label=u'Country', empty_label=u'Not defined')

有更好的答案吗?

1 个答案:

答案 0 :(得分:0)

models.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()

urls.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")),

forms.py

class UserRegistrationForm(RegistrationForm):
    country = forms.ModelChoiceField(queryset=Countries.objects, label=u'Country', empty_label=u'Not defined')