Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7822393
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:54:53+00:00 2026-06-02T07:54:53+00:00

I want to make sure users fill all the fields before they are redirected

  • 0

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?

Models

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 %}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T07:54:54+00:00Added an answer on June 2, 2026 at 7:54 am

    There’s a few issues here, but your biggest issue is your view:

    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))
    

    Taking out the if else block:

        if request.method=="POST":
                form=MembForm(request.POST)
                if form.is_valid():
                        data=form.cleaned_data
                        form.save()
                return HttpResponseRedirect('/good/')
    

    What happens here is, it checks to see if the form is valid. But, no matter if it is valid or not, you still return /good/. This is probably not what you intended to do.

    What you want to do is this:

        if request.method=="POST":
                form=MembForm(request.POST)
                if form.is_valid():
                        data=form.cleaned_data
                        form.save()
                        return HttpResponseRedirect('/good/')
                else:
                    return HttpResponse #something else, or maybe just the form
    

    of course, this can be consolidated together with your code below, so all you really need is:

        if request.method=="POST":
                form=MembForm(request.POST)
                if form.is_valid():
                        data=form.cleaned_data # by the way, what is this for? seems extraneous.
                        form.save()
                        return HttpResponseRedirect('/good/')
    

    A few other things I noticed:

      class MembForm(ModelForm):
            class Meta:
                 model=Memb
                 fields=('slug','member')
    

    You don’t need to explicitly define fields when using a model form. It’s the whole point of using them 😉 There are times to do this, and there are times to exclude them, but in your example I don’t see it.

    I’m also not sure what the method “clean_slug” is getting you that the default functionality doesn’t do better. But, this could just be a snippet.

    Finally, you are redirecting to a page called “good”. This is going to be the same no matter what the user enters, you should ask yourself if this is desired behavior. You may want to redirect to something like /good/(someuniqueid)/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My site allows for resume upload, but I want to make sure users won't
I want to make sure rogue users can't damage my site or database by
Just want to make sure I understand this correctly (I'd ask on SO Chat,
I want to make sure 'grid' can't return 2 same values, but I'm not
I want to make sure people can't type the name of a PHP script
I want to make sure that a set of functions have the same signature
I want to make sure a file path set via query string does not
I want to make sure a string has only characters in this range [a-z]
I want to make sure I'm not inserting a duplicate row into my table
I want to make sure the first 4 letters in a eight character code

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.