我想知道如何更改默认 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}} 的每个字段,以便轻松访问视图。怎么做?
这也是我的 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 的新手??