【发布时间】:2014-08-22 00:35:48
【问题描述】:
刚开始接触 Django,在扩展经典的 UserRegistrationForm 时遇到了困难。我已经按照教程here 进行了操作,这很棒,但是浏览器中的 Html 表单显示了我不想要的字段。我现在只想扩展电子邮件,但想稍后添加名字和姓氏。
请注意我还没有 CSS,现在只想在浏览器中查看基本信息。谁能解释为什么我看到除了 username、password、password2 和 email 之外的所有其他字段?
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
field = ('username','email','first_name','last_name','password1', 'password2')
def save(self, commit=True)
user = super(MyRegistrationForm, self).save(commit=False)
user.email = self.cleaned_data['email'] #validated before committing to database
if commit:
user.save()
return user
views.py
from django.shortcuts import render_to_response #allows you to render a template back to the browser
from django.http import HttpResponseRedirect #allows the browser to redirect to another url
from django.contrib import auth
from django.core.context_processors import csrf # method to stop hackers submitting requests
from fantasymatchday_1.forms import MyRegistrationForm #A user registration form I created that inherits the UserCreationForm
def register_user(request):
if request.method == 'POST':
form = MyRegistrationForm(request.POST) #create a form object
if form.is_valid(): #if the form is valid, save the form
form.save()
return HttpResponseRedirect('/register_success')
args = {}
args.update(csrf(request))
args['form'] = MyRegistrationForm()
#print args
return render_to_response('register.html', args)
def register_success(request):
return render_to_response('register_success.html')
register.html
<h2> Register </h2>
<form action="/register/" method="post">{% csrf_token %}
{{form}}
<input type="submit" value="Register" />
</form>
为什么其他人都出现了?对此的任何帮助将不胜感激:)
【问题讨论】:
标签: django django-forms