coded kid
unread,Apr 15, 2012, 5:22:43 AM4/15/12Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to Django users
I want my form to validate the fields so that only users who fill the
form field should be redirected to the next page. If a user fails to
fill any of the form field, the user should be taken to the same page
where it will output validation error so that the user can fill the
fields.
I tried writing these codes but it’s not working! Even if the user
didn’t fill the form field it will still redirect the user to the next
page which is not meant to be so.
Models.
class Memb(models.Model):
slug=models.CharField(max_length=100)
member=models.CharField(max_length=100)
def __unicode__(self):
return u"%s" % self.member
class MembForm(ModelForm):
class Meta:
model=Memb
fields=('slug','member')
Views:
def my_memb(request):
if request.method=="POST":
form=MembForm(request.POST)
if form.is_valid():
data=form.cleaned_data
form.save()
return HttpResponseRedirect('/test/')
else:
form=MembForm()
return render_to_response('member.html',
{'MembForm':MembForm}, context_instance=RequestContext(request))
Template:
{% extends "base.html" %}
{% block title %} Add Member {% endblock %}
{% block content %}
<form action="" method="POST">
{{MembForm.as_p}}
<input type="submit" value="Add"/>
</form>
{% endblock %}
How can I make sure the form validate before taking the user to the
next page? Kindly put me through. Thanks!