Wednesday, July 28, 2010

Re: User Registration: Looking for tips

For the UserProfle model you've provided you won't be able to user
the UserCreationForm as there are a load of extra fields. Moreover
because User is a foreign key from that model it isn't a straight-
forward matter of using a model form. To get a single model form to
split the work to two underlying models would be a fair bit of work.

Although ModelForm is great, if you want to do things which are a bit
off what they were intended to do I'd use a simple Form and create a
custom save method that writes out the data to the two models. That's
how we do our registration process since we create at least 6 model
instances at join time.

Euan

On 29 July, 06:45, strayhand <tobyb...@gmail.com> wrote:
> I'm using the auth module for my project and I've got a registration
> form that's based on the "UserCreationForm" provided by
> "django.contrib.auth.forms" but it's only showing the username and
> password fields.
>
> I want a registration process that requires users to fill out ALL of
> the "User" fields plus the fields in my "UserProfile" model.
>
> If someone has some basic suggestions for how to modify the
> UserCreationForm to include these additional fields I'm all ears. I
> managed to find some basic examples of how to hide certain ModelForm
> fields or change their labels but nothing about adding additional
> fields, particularly from other models.
>
> In addition to modifying the UserCreationForm I've come across a
> couple of leads:
>
> 1. Build my own registration process following this example:
>
> http://www.b-list.org/weblog/2006/sep/02/django-tips-user-registration/
>
> I'm concerned with how old this post is. Is this approach still valid
> given that it was posted in 2006? The author is using both the User
> model and his own UserProfile model. I think that I could adapt this
> for my purpose.
>
> 2. Use the django-registration package provided by the same author
>
> http://bitbucket.org/ubernostrum/django-registration/overview
>
> I'm concerned with having to install additional packages into Django.
> It seems like this will make deploying my application on a Web host
> more difficult. Can anyone speak to this?
>
> Thank you for your suggestions. I've provided my code thus far below.
>
> # forms.py
>
> from django import forms
> from django.contrib.auth.forms import UserCreationForm
> from django.http import HttpResponseRedirect
> from django.shortcuts import render_to_response
> from django.template import RequestContext
>
> def register(request):
>         if request.method == 'POST':
>                 form = UserCreationForm(request.POST)
>                 if form.is_valid():
>                         new_user = form.save()
>                         return HttpResponseRedirect("/somewhere/")
>         else:
>                 form = UserCreationForm()
>         return render_to_response("registration/register.html",
> {'form':form,}, context_instance=RequestContext(request))
>
> # profiles.models.py
>
> from django.db import models
> from django.contrib.auth.models import User
> from ylbbq.areas.models import Area
>
> class UserProfile(models.Model):
>     user = models.ForeignKey(User, unique=True)
>     phone = models.CharField(max_length=12)
>     address = models.CharField(max_length=70)
>     city = models.CharField(max_length=50)
>
>     STATES = (
>         ('AK', 'Alaska'),
>            ...
>         )
>
>     state_province = models.CharField(max_length=2, choices=STATES,
> verbose_name='State or Province')
>
>     COUNTRIES = (
>         ('USA', 'United States'),
>         ('Canada', 'Canada')
>     )
>
>     country = models.CharField(max_length=20, choices=COUNTRIES)
>     zip_code = models.CharField(max_length=5)
>     birth_date = models.DateField()
>     areas = models.ManyToManyField(Area)
>
> # templates/registration/register.html
>
> {% extends "base.html" %}
>
> {% block title %} Account Registration {% endblock %}
>
> {% block content %}
>         <h1>Create an account</h1>
>
>         <form action="" method="post">{% csrf_token %}
>                 {{ form.as_p }}
>                 <input type =submit value="Create Account" />
>         </form>
>
> {% endblock %}

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment