0

I am using the django registratations.backends.simple.urls and I want to add a customised field in my registration page

The simple.backend is different from the default.backend as it's lightweight and doesn't require user verification configurations("smtp, email, etc")

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')

is there a better answer?

laycat
  • 5,381
  • 7
  • 31
  • 46
  • Possible duplicate of http://stackoverflow.com/questions/2601487/django-registration-django-profile-using-your-own-custom-form – Talvalin Jun 25 '13 at 08:20
  • Did it, will be updating the solution soon. This is not a duplicate as this utilises the Simple.urls as compared to the other post which utilises the Default.Urls – laycat Jun 25 '13 at 08:37
  • What was the difference between the two solutions? Just the config in urls.py or something more? – Talvalin Jun 25 '13 at 09:39
  • you are right, the main difference is in urls.py however I utilised the the user_registered signal as compared to the accepted solution. – laycat Jun 25 '13 at 09:53

1 Answers1

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')
laycat
  • 5,381
  • 7
  • 31
  • 46