[CentralOH] django.contrib.auth

Jason Champion jchampion at zetacentauri.com
Fri Oct 2 20:33:02 CEST 2009


Here's how I do it (sorry, can't read access b-list behind the firewall I'm
behind).  It's not extremely elegant, but it works well enough (snipped out
some non-relevant or site-specific chunks, hopefully it's still clear):

MODEL:

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    street_address = models.CharField(max_length=80, null=True, blank=True)
    city = models.CharField(max_length=40, null=True, blank=True)
    state = models.CharField(max_length=2, null=True, blank=True)
    zip = models.IntegerField(max_length=9, null=True, blank=True)
    avatar = models.ImageField(null=True, blank=True,
upload_to='images/avatars$

    def __unicode__(self):
        return self.user.username

VIEW:

def register(request):
    user = User()
    profile = UserProfile(user=user)

    if request.method == 'POST':
        user_form = RegistrationForm(request.POST, instance=user)
        profile_form = ProfileForm(request.POST, instance=profile)
        if user_form.is_valid() and profile_form.is_valid():
            # Check for email address already used.
            email = request.POST['email']
            duplicate_email = User.objects.filter(email=email)
            if( duplicate_email ):
                return render_to_response('register.html', {'user_form':
user_form, 'profile_form': profile_form, 'error': 'Error: Email address is
already in use.'}, context_instance=RequestContext(request))

            # Require both passswords to match.
            pwd1 = request.POST['password1']
            pwd2 = request.POST['password2']
            if pwd1 != pwd2:
                return render_to_response('register.html', {'user_form':
user_form, 'profile_form': profile_form, 'error': 'Passwords do not
match.'}, context_instance=RequestContext(request))
            user = user_form.save()
            user.set_password( request.POST['password1'] )
            user.save()
            profile_form.cleaned_data['user'] = user
            profile = profile_form.save()
            return HttpResponseRedirect("/home/")
        else:
            return render_to_response('register.html', {'user_form':
user_form, 'profile_form': profile_form},
context_instance=RequestContext(request))
    else:
        user_form = RegistrationForm(instance=user)
        profile_form = ProfileForm(instance=profile)
        return render_to_response('register.html', {'user_form': user_form,
'profile_form': profile_form }, context_instance=RequestContext(request))

TEMPLATE BODY:

  {% if error %}
    <p class="error">{{ error }}</p>
  {% endif %}
  {% if user_form.errors %}
    <p class="error">{{ user_form.errors }}</p>
  {% endif %}
  {% if profile_form.errors %}
    <p class="error">{{ profile_form.errors }}</p>
  {% endif %}

  <form action='.' method='post'><center><table>
    <tr><td><label for="username">User name:</label>
    </td><td><input type="text" name="username" value=""
id="username"></td></tr>
    <tr><td><label for="email">Email:</label>
    </td><td><input type="text" name="email" value="" id="email"></td></tr>
    <tr><td><label for="password1">Password:</label>
    </td><td><input type="password" name="password1" value=""
id="password1"></td></tr>
    <tr><td><label for="password2">Password Again:</label>
    </td><td><input type="password" name="password2" value=""
id="password2"></td></tr>
    <tr><td colspan="2"><center><input type="submit" value="Register" />
    <input type="hidden" name="next" value="{{ next }}" /></center></td>
  </table></center></form>

HTH,
Jason

On Fri, Oct 2, 2009 at 12:40 PM, miles groman <miles.groman at gmail.com>wrote:

> Hi - Django question (Im on v1.1):
>
> While working on some Django code, I found myself needing more then
> what the User model provides.
>
> A quick google search brings up the following:
>
> http://www.b-list.org/weblog/2006/jun/06/django-tips-extending-user-model/
>  - I would normally, without a doubt, trust what James Bennet says on
> Django, but this is over 3 years old...
>
> Is this still the best way to extend User functionality?  The
> documentation from djangoproject.org and the Django book seem to
> agree.
>
> Has anyone accomplished this a different way ?
>
>
> miles
> _______________________________________________
> CentralOH mailing list
> CentralOH at python.org
> http://mail.python.org/mailman/listinfo/centraloh
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/mailman/private/centraloh/attachments/20091002/2860eb38/attachment.htm>


More information about the CentralOH mailing list