Search code examples
djangodjango-registration

customise form-fields of django-registration utilising the simple.backend


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?


Solution

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