刚开始进入Django并在扩展经典UserRegistrationForm时陷入困境。我已经按照教程here这很好,但浏览器中的Html表单显示了我不想要的字段。我现在只想扩展电子邮件,但想稍后添加名字和姓氏。
请注意我还没有CSS,只想在浏览器中查看基本信息。除了用户名,密码,密码2 和电子邮件之外,还有人可以解释为什么我会看到所有其他字段吗?
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>
为什么所有其他人都出现了?任何有关这方面的帮助将不胜感激:)
答案 0 :(得分:0)
您的MyRegistrationForm
Meta
定义中有一个拼写错误:
field = ('username','email','first_name','last_name','password1', 'password2')
应该是fields
而不是field
。