FAQ
I want to make sure users fill all the fields before they are
redirected to the next page. And if they don’t fill the fields it
should raise an error telling them to fill the fields before they
proceed. So to do that, I wrote the codes below. But the problem I’m
facing is that when I didn’t fill the fields, it took me to the next
page, instead of it to return me to the same page, and it didn’t raise
any error.

How can I make it validate those fields before taking users to the
next page?

Model:

from django.core.exceptions import ValidationError

class Memb(models.Model):
slug=models.CharField(max_length=100)
member=models.CharField(max_length=100)

def __unicode__(self):
return self.member, self.slug

def clean_slug(self):
data=self.cleaned_data['slug']
if "Testy" not in data:
raise ValidationError("Enter the correct name for this
field")

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('/good/')
else:
form=MembForm()
return render_to_response('member.html',{'MembForm':MembForm},
context_instance=RequestContext(request))

Template:

{% block content %}
<form action="" method="POST">
{{MembForm.as_p}}
<input type="submit" value="Add"/>
</form>
{% endblock %}

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Search Discussions

  • Mario Gudelj at Apr 17, 2012 at 9:28 pm
    Indent that redirect one more time so that it's wothin the if loop and
    you're redirected to good only when the form is valid
    On 18/04/2012 5:07 AM, "coded kid" wrote:

    I want to make sure users fill all the fields before they are
    redirected to the next page. And if they don’t fill the fields it
    should raise an error telling them to fill the fields before they
    proceed. So to do that, I wrote the codes below. But the problem I’m
    facing is that when I didn’t fill the fields, it took me to the next
    page, instead of it to return me to the same page, and it didn’t raise
    any error.

    How can I make it validate those fields before taking users to the
    next page?

    Model:

    from django.core.exceptions import ValidationError

    class Memb(models.Model):
    slug=models.CharField(max_length=100)
    member=models.CharField(max_length=100)

    def __unicode__(self):
    return self.member, self.slug

    def clean_slug(self):
    data=self.cleaned_data['slug']
    if "Testy" not in data:
    raise ValidationError("Enter the correct name for this
    field")

    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('/good/')
    else:
    form=MembForm()
    return render_to_response('member.html',{'MembForm':MembForm},
    context_instance=RequestContext(request))

    Template:

    {% block content %}
    <form action="" method="POST">
    {{MembForm.as_p}}
    <input type="submit" value="Add"/>
    </form>
    {% endblock %}

    --
    You received this message because you are subscribed to the Google Groups
    "Django users" group.
    To post to this group, send email to django-users@googlegroups.com.
    To unsubscribe from this group, send email to
    django-users+unsubscribe@googlegroups.com.
    For more options, visit this group at
    http://groups.google.com/group/django-users?hl=en.
    --
    You received this message because you are subscribed to the Google Groups "Django users" group.
    To post to this group, send email to django-users@googlegroups.com.
    To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
  • Tom Evans at Apr 18, 2012 at 8:47 am

    On Tue, Apr 17, 2012 at 8:07 PM, coded kid wrote:
    I want to make sure users fill all the fields before they are
    redirected to the next page. And if they don’t fill the fields it
    should raise an error telling them to fill the fields before they
    proceed. So to do that, I wrote the codes below. But the problem I’m
    facing is that when I didn’t fill the fields, it took me to the next
    page, instead of it to return me to the same page, and it didn’t raise
    any error.

    How can I make it validate those fields before taking users to the
    next page?

    Model:

    from django.core.exceptions import ValidationError

    class Memb(models.Model):
    slug=models.CharField(max_length=100)
    member=models.CharField(max_length=100)

    def __unicode__(self):
    return self.member, self.slug
    Slight nit. The __unicode__ method must always return unicode. Strange
    things will happen if it doesn't return unicode, but instead returns a
    2-tuple.

    Cheers

    Tom

    --
    You received this message because you are subscribed to the Google Groups "Django users" group.
    To post to this group, send email to django-users@googlegroups.com.
    To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupdjango-users @
categoriesdjango, python
postedApr 17, '12 at 7:07p
activeApr 18, '12 at 8:47a
posts3
users3
websitedjangoproject.com

People

Translate

site design / logo © 2022 Grokbase