0

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??

user2823667
  • 193
  • 2
  • 18
  • You don't access the view from the template. Also (it might be a typo) but the first file you printed is the view, not urls. The view assembles a context (dictionary) of attributes and values and renders the template ,returning the rendered information (usually HTML) to the user. What are you trying to do in the template? – Timmy O'Mahony Oct 07 '13 at 11:24
  • I just want to beautify my registration form> – user2823667 Oct 07 '13 at 11:40
  • How to do that ?? I want to apply some CSS on the fields of that form ?? And the same time i don'w want to change the functionalities of that form i.e. the form must do the same functionality as before.... – user2823667 Oct 07 '13 at 11:43

2 Answers2

3

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.

OBu
  • 4,977
  • 3
  • 29
  • 45
1

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.

Rob L
  • 3,634
  • 2
  • 19
  • 38
  • can you please elaborate on that ?? i don't know how to do that?? Isn't there any method to customize that form?? – user2823667 Oct 07 '13 at 13:16
  • The form is just a Django template. You can customize it with JavaScript, CSS and HTML. – Rob L Oct 07 '13 at 13:18
  • yeah but hoe to access the fields of that form ?? what i mean is in password field it is displaying some extra things how to make the password field to display things with moere efficiency and for another case how will i apply some class on the button generated by the form ?? – user2823667 Oct 07 '13 at 13:21
  • Applying css to the form is dead simple. Include the .css file that has the styles in it. But you don't really need to write your own views to handle the form. Django already does all that for you. – Rob L Oct 07 '13 at 13:22
  • yeah but how to apply css on the fields of the form like password field,username field,etc – user2823667 Oct 07 '13 at 13:24
  • Well, look at the id of the username field, for example. It's usually `id_username`. So css would be `#id_username { color: red; }` – Rob L Oct 07 '13 at 13:27
  • Thanks i think it will be useful :) – user2823667 Oct 07 '13 at 13:32