views:

57

answers:

2

In my django project I need to add registration function. Problem is that in registration process I can't use a 'userprofile' anywhere. My user is defined by 'first name' , 'last name' and some other data. How to achieve this ? Apart of enabling contrib.auth and 'registration' I've created a 'user' application. In user.models I have an extended user model with additional fields. In user.forms I have created extended registration form :

class ExtendedRegistrationForm(RegistrationForm):
    first_name = forms.CharField(label="First name", error_messages={'required': 'Please fill the first name field'})
    last_name = forms.CharField(label="Last name", error_messages={'required': 'Please fill the last name field'})


    def save(self, profile_callback=None):
        user = super(ExtendedRegistrationForm, self).save()
        user.first_name = self.cleaned_data['first_name']
        user.last_name = self.cleaned_data['last_name']
        user.save()

In user.views I have a custom register view :

def custom_register(request, success_url=None,
           form_class=ExtendedRegistrationForm, profile_callback=None,
           template_name='registration/registration_form.html',
           extra_context=None):

            def _create_profile(user):                
                p = UserProfile(user=user)
                p.is_active = False
                p.first_name = first_name
                p.last_name = last_name
                p.save()

            return register(request, success_url="/accounts/register/complete",
                      form_class=ExtendedRegistrationForm, profile_callback=_create_profile,
                      template_name='registration/registration_form.html',
                      extra_context=extra_context)

and also I've overriden registration urls for my project :

url(r'^accounts/password/reset/$',
       auth_views.password_reset, { 'post_reset_redirect' : '/',
       'email_template_name' : 'accounts/password_reset_email.html' },
       name='auth_password_reset', ),
url(r'^accounts/password/reset/confirm/(?P<uidb36>[0-9A-Za-z]+)-(?P<token>.+)/$',
       auth_views.password_reset_confirm, { 'post_reset_redirect' : '/accounts/login/'},
       name='auth_password_reset_confirm'),
url(r'^accounts/password/reset/complete/$',
       auth_views.password_reset_complete,
       name='auth_password_reset_complete'),
url(r'^accounts/password/reset/done/$',
       auth_views.password_reset_done,
       name='auth_password_reset_done'),
url(r'^accounts/register/$',
    'user.views.custom_register',
    name='registration_register'),
(r'^accounts/', include('registration.urls')),

So I have a good base to start but how to get rid of 'username' ? Can I just treat username as first_name (so many users with the same name) or will django complain ?

A: 

When I had to tackle this, the easiest way is to not include the "extended user profile" stuff in the registration process. When they log in for the first time, redirect them or send them a message to fill out forms. This should at least get you going on it. I'm going to be tackling this myself soon enough, so when I find a more concrete solution, I'll post it.

I'm still also not sure what you mean by being unable to access username... that's part of auth.models.User, so it IS available. Are you ignoring the basic fields already available in User?...

eruciform
A: 

Why not generate a username on save, based on first_name and last_name?

Lexo