Re: form.errors
Its just displaying form again, but without any errors. Please guide me to where am missing something.
On Fri, Jul 12, 2013 at 4:34 AM, Kakar Arunachal Service <kakararunachalservice@gmail.com> wrote:
I am sorry, but its still not displaying any errors after it renders.On Fri, Jul 12, 2013 at 3:47 AM, carlos <crocha09.09@gmail.com> wrote:
try this with django 1.5.1from django.shortcuts import renderform.save()def register_page(request):
if request.method == 'POST':
form = RegistrationForm(request.POST)
if form.is_valid():
return render(request, 'registration/register.html', {'form':form})
return HttpResponseRedirect('/register/success/')
else:
form = RegistrationForm()
Cheers--On Thu, Jul 11, 2013 at 4:08 PM, Kakar Arunachal Service <kakararunachalservice@gmail.com> wrote:--And my register.html:And this is my views.py:Hi,I am learning Django from an old Django version book, and i am stuck with a problem regarding forms. When i render forms, i get the registration right, but if its incorrect, it does not show the error msg in the html. I tried {{form.errors}}, but couldn't fix he problem. Please guide me.
Thank you.
This is my forms.py:
from django import forms
import re
from django.contrib.auth.models import User
from django.core.exceptions import ObjectDoesNotExist
class RegistrationForm(forms.Form):
username = forms.CharField(label='Username', max_length=30)
email = forms.EmailField(label="Email",)
password1 = forms.CharField(
label='Password',
widget=forms.PasswordInput()
)
password2 = forms.CharField(
label = 'Password (Again)',
widget=forms.PasswordInput()
)
def clean_password2(self):
if 'password1' in self.cleaned_data:
password1 = self.cleaned_data['password1']
password2 = self.cleaned_data['password2']
if password1 == password2:
return password2
raise forms.ValidationError('Password do not match.')
def clean_username(self):
username = self.cleaned_data['username']
if not re.search(r'^\w+$', username):
raise forms.ValidationError('Username can only contain alphanumeric characters and the underscore.')
try:
User.objects.get(username=username)
except ObjectDoesNotExist:
return username
raise forms.ValidationError('Username is already taken.')
def clean_email(self):
if 'email' in self.cleaned_data:
email = self.cleaned_data['email']
try:
User.objects.get(email=email)
except ObjectDoesNotExist:
return email
raise forms.ValidationError('Email is already taken.')
def register_page(request):
if request.method == 'POST':
form = RegistrationForm(request.POST)
if form.is_valid():
user = User.objects.create_user(
username=form.cleaned_data['username'],
email=form.cleaned_data['email'],
password=form.cleaned_data['password1']
)
return HttpResponseRedirect('/register/success/')
else:
form = RegistrationForm()
variables = RequestContext(request,{
'form':form
})
return render_to_response('registration/register.html',variables)
else:
form = RegistrationForm()
variables = RequestContext(request,{
'form':form
})
return render_to_response('registration/register.html',variables)
{% extends "base.html" %}
{% block title %}User Registration{% endblock %}
{% block head %}User Registration{% endblock %}
{% block content %}
<form method="POST" action=".">
{% csrf_token %}
{{form.as_p}}
<input type="submit" value="register" />
</form>
{% endblock %}
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home