我需要你的帮助。 问题:我有一个用于创建新用户的模板,视图和表单,如果我在注册时出现相同的错误(例如:password1!= password2),我应该在模板中看到错误,但它们不是。 SOS!
观点:
def registration(request):
if request.method == "POST" :
form = UserCreationForm(request.POST, request.FILES)
if form.is_valid():
print "form is valid"
form.save()
return render_to_response("action_succes.html")
else:
print form.is_valid()
print form.errors
form = UserCreationForm(request.POST)
else:
form = UserCreationForm()
args = {}
args.update(csrf(request))
form = UserCreationForm()
#args["form"] = UserCreationForm()
#return render_to_response("registration.html", args)
return render(request, "registration.html", {'form':form})
模型:
class User(AbstractBaseUser):
email = models.EmailField(
verbose_name='Email address',
max_length=45,
unique=True,
db_index=True,
)
username = models.TextField(verbose_name='Nick', max_length=15, unique=True)
profession = models.TextField(verbose_name='Profession', max_length=15)
avatar = models.FileField(upload_to=get_upload_file_name, blank=True, null=True)
profile_vkontakte = models.TextField(verbose_name='Profile in vkontakte', max_length=10, null=True)
coordinate_x = models.FloatField(verbose_name='X', null=True)
coordinate_y = models.FloatField(verbose_name='Y', null=True)
is_active = models.BooleanField(default=True)
is_admin = models.BooleanField(default=False)
objects = UserManager()
USERNAME_FIELD = 'username'
REQUIRED_FIELDS = ['email']
def get_full_name(self):
return '%s %s' % (self.first_name, self.last_name,)
def get_short_name(self):
return self.username
def __unicode__(self):
return self.email
def has_perm(self, perm, obj=None):
return True
def has_module_perms(self, app_label):
return True
@property
def is_staff(self):
return self.is_admin
模板:
<form action="registration" method="POST" enctype="multipart/form-data">{%csrf_token%}
<label for="id_email">Email</label><br>
{{form.email}}<br>
<label for="id_username">Username</label><br>
{{form.username}}<br>
<label for="id_profession">Profession</label><br>
{{form.profession}}<br>
<label for="id_avatar">Avatar</label><br>
{{form.avatar}}<br>
<label for="id_password1">Password</label><br>
{{form.password1}}<br>
<label for="id_password2">Password (confirm)</label><br>
{{form.password2}}<br>
<label for="id_profile_vkontakte">Vkontakte ID: </label><br>
{{ form.profile_vkontakte }}<br>
<br><br>
Choice you current location:<br>
<label for="id_coordinate_x">X: </label><br>
{{ form.coordinate_x }}<br>
<label for="id_coordinate_y">Y: </label><br>
{{ form.coordinate_y }}<br>
{% for i in form.errors %}
{{i}}
{% endfor %}
{% if form.errors %}
error!
{% endif %}
<input type="submit" value="Register"/><br>
答案 0 :(得分:5)
你已经分配; form = UserCreationForm()的次数超过了所需的次数。
应该是:
if request.method == "POST" :
form = UserCreationForm(request.POST, request.FILES) #since request.POST is present, populate the form with POST values
if form.is_valid(): #Check if its valid
print "form is valid"
form.save()
return render_to_response("action_succes.html")
else: #invalid case
print form.is_valid() #form contains data and errors
print form.errors
else:
form = UserCreationForm() #No post data
args = {}
args.update(csrf(request))
return render(request, "registration.html", {'form':form}) #Sending the form to view