我想知道如何更改默认UserRegistrationForm的显示方式。 这是我的views.py文件。
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 %}
我想独立访问{{form}}的每个字段,以便轻松访问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 :(得分:3)
您可以执行以下操作:
appname/templates/registration
- 文件夹django/contrib/admin/templates/registration (or ../admin)
- 文件夹中的原始表单可能是一个好主意,以了解原始模板中的操作。{% extends "base.html" %}
进行扩展。 将观看次数添加到urls.py
,例如:
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中定义任何内容。
答案 1 :(得分:1)
所以制作自己的表格:
从Django-x.x/django/contrib/admin/templates
复制base.html, base_site.html and login.html
到project_name/templates/admin
然后根据需要更改文件。