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 8098413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T22:04:24+00:00 2026-06-05T22:04:24+00:00

I am a real beginner in web development. The following code is failing at

  • 0

I am a real beginner in web development. The following code is failing at the is_valid() check. But I do not understand why: The form should get its data filled from the POST-data or not?

Model:

class Statement(models.Model):
    text = models.CharField(max_length=255)
    user = models.ForeignKey(User)
    time = models.DateField()
    views = models.IntegerField()

ModelForm:

class StatementForm(ModelForm):
    class Meta: 
        model = Statement
        widgets = {
                   'time':forms.HiddenInput(),
                   'user':forms.HiddenInput(), 
                   'views':forms.HiddenInput(), 
        }

View function:

def new(request):  
    if request.method == 'POST': # If the form has been submitted...
        form = StatementForm(request.POST) # A form bound to the POST data
        if form.is_valid():
            stmt = form.save()
            path = 'stmt/' + stmt.id
            return render_to_response(path, {'stmt': stmt})
    else:  
        c = {}
        c.update(csrf(request))
        loggedin_user = request.user 
        d = datetime.now()
        form = StatementForm(request.POST, initial={'time': d.strftime("%Y-%m-%d %H:%M:%S"), 'user':loggedin_user, 'views':0})
        return render_to_response('new_stmt.html', {'form': form, },context_instance=RequestContext(request))

I found similar topics and tried a lot. This is how i think it should work. I really need advice.

  • 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-05T22:04:25+00:00Added an answer on June 5, 2026 at 10:04 pm

    All fields of your model are required. So, form.is_valid() will be True, if all fields are filled with correct values and are not blanked.
    You have declared fields time, user, views as hidden fields. Are you sure, that you have filled them in your template form?
    Also, you may want to auto stamp field time = models.DateField(). Modify your model field like

    time = models.DateField(auto_now=True)`. 
    

    After this you don’t have to fill it by yourself in template form.

    Your view must return HttpResponse object in all cases. If your form is not valid, i.e. if form.is_valid() will return False, then no HttpResponse object will be returned by your view. This can be the source of your fail. Add else statement for if form.is_valid():

    from django.http import Http404
    def new(request):  
        if request.method == 'POST': # If the form has been submitted...
            form = StatementForm(request.POST) # A form bound to the POST data
            if form.is_valid():
                stmt = form.save()
                path = 'stmt/' + stmt.id
                return render_to_response(path, {'stmt': stmt})
            else:
                # Do something in case if form is not valid
                raise Http404 
        else: 
            # Your code without changes
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm interested in wed development and web 2.0 but I'm a real beginner, so
I am a C++ beginner. I have the following code, the reult is not
I am a beginner and I cannot understand the real effect of the Iterable
Real noob question no doubt, but I can't get my head around this behaviour.
I'm a beginner in practical real-world java programming. I don't really understand these basic
Real odd one to get stuck on but weirdly I am. You you have
I want to begin learning embedded linux development (i'm not beginner in C nor
probably this is a real beginner question, but i cannot find an answer on
I'm following a tutorial. (Real World Haskell) And I have one beginner question about
A real F# noob question, but what is |> called and what does it

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.