2013-10-07 58 views
0

我想知道如何更改默认UserRegistrationForm的显示。 这是我的views.py文件。如何自定义Django的UserRegistration表单?

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') 

这是在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 %} 

我要访问的每一个申请的{{形式}}独立,使得它很容易访问view.how办呢?

而且这是我的forms.py文件

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 

请帮助我,我是新来的Django?

+0

您不能从模板访问视图。另外(它可能是一个错字),但你打印的第一个文件是视图,而不是网址。该视图组合了属性和值的上下文(字典)并呈现模板,并将呈现的信息(通常为HTML)返回给用户。你想在模板中做什么? –

+0

我只想美化我的登记表> – user2823667

+0

如何做到这一点?我想在该表单的字段上应用一些CSS? 与此同时,我不想更改该表单的功能,即表单必须执行与之前相同的功能.... – user2823667

回答

3

你可以做到以下几点:

  1. 创建appname/templates/registration -folder
  2. 在这个文件夹,把所有你想拥有的HTML模板(如login.html的,password_change_form.html,... )。查看您的django/contrib/admin/templates/registration (or ../admin)-文件夹中的原始表单以了解原始模板中所做的操作可能是一个不错的主意。
  3. 根据您的需求自定义模板。如果你想将ssame css应用到每一个页面,我会建议编写你自己的base.html并使用{% extends "base.html" %}来扩展它。
  4. 添加的意见,你的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'), 
    

没有必要来定义forms.py或views.py什么。

+0

谢谢!我认为它可能工作.... :) – user2823667

1

因此使自己的形式:

Django-x.x/django/contrib/admin/templates副本base.html, base_site.html and login.htmlproject_name/templates/admin

然后,更改文件是必要的。

+0

请您详细说明一下吗? 我不知道该怎么做? 是不是有任何方法来定制这种形式? – user2823667

+0

表单只是一个Django模板。您可以使用JavaScript,CSS和HTML进行自定义。 –

+0

是的,但锄头访问该窗体的字段? 我的意思是在密码领域中,显示了一些额外的东西 如何使密码字段,以显示我将如何适用于通过形式产生的按钮有些类默勒效率 和另一种情况的事情? – user2823667