user2823667
user2823667

Reputation: 193

How to customize UserRegistration Form of Django?

I want to know how to change the display of default UserRegistrationForm. This is my views.py file.

from django.http import *
from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
from django.contrib import auth
from django.core.context_processors import csrf
from django.contrib.auth.forms import UserCreationForm
from forms import MyRegistrationForm


def register_user(request):
    if request.method == 'POST':
        form = MyRegistrationForm(request.POST)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/accounts/register_success')
    args = {}
    args.update(csrf(request))

    args['form'] = MyRegistrationForm()
    return render_to_response('register.html', args)

def register_success(request):
    return render_to_response('register_success.html')

This is what is displayed in templates of register_user.

{% extends "application/base.html" %}

{% block content %}
    <h2> Register </h2>
    <form action="/accounts/register/" method="post">{% csrf_token %}
        {{form}}
        <input type="submit" value="Register" />
    </form>
{% endblock %}

I want to access each and every filed of the {{form}} independently so that the it is easy to access the view.how to do it??

Also this is my forms.py file

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


class MyRegistrationForm(UserCreationForm):
    email = forms.EmailField(required=True)

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

    def save(self,commit=True):
        user=super(MyRegistrationForm, self).save(commit=False)
        user.email=self.cleaned_data['email']
        if commit:
            user.save()
        return user

Please Help me i am new to django??

Upvotes: 0

Views: 4283

Answers (2)

OBu
OBu

Reputation: 5167

You can do the following:

  1. create appname/templates/registration-folder
  2. In this folder, put all the html-templates you want to have (e.g. login.html, password_change_form.html, ...). It might be a good idea to take a look at the original forms in your django/contrib/admin/templates/registration (or ../admin)-folder to get an idea of what's done in the original templates.
  3. Customize the templates to your needs. If you want to apply the ssame css to every page, I would suggest writing your own base.html and extend it using {% extends "base.html" %}.
  4. Add the views to your urls.py, eg.g.:

    url(r'^accounts/login/$', 'django.contrib.auth.views.login'),
    url(r'^accounts/logout/$', 'django.contrib.auth.views.logout_then_login'),
    url(r'^accounts/password_change_done/$', 'django.contrib.auth.views.password_change_done', name="password_change_done"),
    url(r'^accounts/password_change/$', 'django.contrib.auth.views.password_change', name='password_change'),
    

There is no need to define anything in forms.py or views.py.

Upvotes: 3

Rob L
Rob L

Reputation: 3734

So make your own form:

From Django-x.x/django/contrib/admin/templates copy base.html, base_site.html and login.html to project_name/templates/admin

Then change the files as necessary.

Upvotes: 1

Related Questions