Skip to content
This repository was archived by the owner on Dec 9, 2024. It is now read-only.

User Registration

Wenzhong Duan edited this page May 31, 2017 · 28 revisions

User registration:

The first few steps for setting up user registration is like user login:

Routing the URLS:

Add the url in urlpatterns in app/urls.py

url(r'^register/$', app_views.register, name='register'),

Writing a register view

This time, we are going to create a view for or user registration. Go to app/views.py:

def register(request):
    if request.method == 'POST':
        form1 = RegistrationForm(request.POST)
        form2 = AdditionalForm(request.POST)
        if form1.is_valid() and form2.is_valid():
            model1 = form1.save()
            model2 = form2.save(commit=False)
            model2.user = model1
            model2.save()
            return redirect('index')
    else:
        form1 = RegistrationForm()
        form2 = AdditionalForm()
    return render(request, 'app/register.html', {'form1': form1, 'form2': form2})

We use two forms to manage user information. form1 will store required information such as username and password, and form2 the optional information. The reason we have commit=False in model2 = form2.save(commit=False) is that we do not want form2 to create a new user, so when we create form2, it does not sync to the database, it allows us to make changes before we sync them. After we assign the form1.user to form2.user, we call model2.save() and the data goes to the database. After registration, we will redirect the user to our home page.

Creating forms

Now it's time to set up our forms. create a file forms.py' in appand importfroms` from Django.

from django import forms

We are going to use the form UserCreationForm provided by Django as our form1, the form contains basic information we must know about our users. The form also needs a model called User.

from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User

From the function above, we assigned form1 as RegistrationForm. We could simply use UserCreationForm, but it only comes with username and password and we do want to know more about our user. Therefore, we are going to extend the UserCreationForm by creating a form of our own:

class RegistrationForm(UserCreationForm):
    first_name = forms.CharField(max_length=30)
    last_name = forms.CharField(max_length=30)
    email = forms.EmailField(max_length=254)

    class Meta:
        model = User
        fields = ('username', 'first_name', 'last_name', 'email', 'password1', 'password2')

In our RegistrationForm, we defined 2 CharField and a EmailField. If we do not specify required=False such as

email = forms.EmailField(max_length=254, required=False)

then it will be a required information, and it will produce an error if users do not fill in in an acceptable format(such as "12345" in EmailField).

If we want to put some help text next to it, we can do

email = forms.EmailField(max_length=254, help_text='Required.')

and then add the following in the template:

{% if field.help_text %}
  {{ field.help_text }}
{% endif %}





from .models import RegisterUser

Clone this wiki locally