如何自定义Django的UserRegistration表单? [英] How to customize UserRegistration Form of Django?

查看:118
本文介绍了如何自定义Django的UserRegistration表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何更改默认UserRegistrationForm的显示。
这是我的views.py文件。

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

$ b $ 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()
返回render_to_response('register.html',args)

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

这就是显示内容
$ b

  {%extendsapplication / base.html%} 

{%}块内容%}
< h2>注册< / h2>
< form action =/ accounts / register /method =post> {%csrf_token%}
{{form}}
< input type =submitvalue =注册/>
< / form>
{%endblock%}

我想访问每个{{ form}},这样就很容易访问view.how做到这一点??



这也是我的forms.py文件

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


类MyRegistrationForm(UserCreationForm):
email = forms.EmailField(required = True)
$ b $ 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']
如果提交:
user.save()
返回用户

请帮助我,我是django的新手??

解决方案

您可以执行以下操作:


  1. 创建 appname / templates / registration -folder

  2. 在此文件夹中,放置您想要的所有html模板(例如login.html,password_change_form.html,...)。查看 django / contrib / admin / templates / registration(或../admin)-文件夹中的原始表单以获取了解原始模板中所做的工作。

  3. 根据需要自定义模板。如果你想将ssame css应用到每一个页面,我会建议编写你自己的base.html,并使用 {%extendsbase.html%} 来扩展它。
  4. 将视图添加到 urls.py 中,例如:

    b $ b

      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 = ),
    url(r'^ accounts / password_change / $','django.contrib.auth.views.password_change',name ='password_change'),



    无需在forms.py或views.py中定义任何内容。


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

    解决方案

    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.

    这篇关于如何自定义Django的UserRegistration表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆