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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:09:31+00:00 2026-05-18T20:09:31+00:00

I’m making a small project to learn Django, and i’m having some problems trying

  • 0

I’m making a small project to learn Django, and i’m having some problems trying to use hidden fields inside a ModelForm.

Theres the class:

class Post(models.Model):
    title       = models.CharField(max_length = 140)
    message     = models.CharField(max_length = 10000)
    pub_date    = models.DateTimeField()

And my ModelForm:

class PostForm(ModelForm):
    class Meta:
        model = Post
        fields = ('title', 'message', 'pub_date',)
        widgets = {
            'message' : Textarea(attrs={'cols':80, 'rows':20}),
            'pub_date' : HiddenInput(),
        }

This is the view:

def edit(request, post_id):
    if request.method == 'POST':
        form = PostForm(request.POST, request.FILES)
        if form.is_valid():
            print 'VALID'
            dados = form.cleaned_data
            post = Post.objects.get(pk=post_id)
            post.title = dados['title']
            post.message = dados['message']
            post.save()
            return HttpResponseRedirect(reverse('blog.views.detail', args=(post.id,)))
        else:
            print 'INVALID'
    form = PostForm(instance=Post.objects.get(pk=post_id))
    return render_to_response('blog/add_post.html', {'form' : form,}, context_instance = RequestContext(request))

This is the HTML generated for the form:

<div style='display:none'>
<input type='hidden' name='csrfmiddlewaretoken' 
value='3b7c0735bf464fb4fd7dcd3011050e1c' />

</div>
<tr>
    <th><label for="id_title">title:</label></th>
    <td><input id="id_title" type="text" name="title" value="asd1111111111" maxlength="140" /></td>
</tr>
<tr>
    <th><label for="id_message">message:</label></th>
    <td><textarea id="id_message" rows="20" cols="80" name="message">asd11111111111</textarea>
    <input type="hidden" name="pub_date" value="2010-12-19 17:08:22.498000" id="id_pub_date" /></td>
</tr>

I use the same template to both create and edit posts. When i’m creating, pub_date is set in another view, and when i’m editing, I can’t change pub_date on my template (thus the hidden field).
On this example, form.is_valid() always return False.
If I remove the 'pub_date' : HiddenInput(), line from my form, it returns True.
What am I doing wrong?

  • 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-05-18T20:09:31+00:00Added an answer on May 18, 2026 at 8:09 pm

    The problem is in the conversion from string to datetime object.

    The datetime field, if no input_formats argument is specified, takes these formats for string->datetime conversion.

    (from the docs: http://docs.djangoproject.com/en/dev/ref/forms/fields/#datetimefield)

    '%Y-%m-%d %H:%M:%S',     # '2006-10-25 14:30:59'
    '%Y-%m-%d %H:%M',        # '2006-10-25 14:30'
    '%Y-%m-%d',              # '2006-10-25'
    '%m/%d/%Y %H:%M:%S',     # '10/25/2006 14:30:59'
    '%m/%d/%Y %H:%M',        # '10/25/2006 14:30'
    '%m/%d/%Y',              # '10/25/2006'
    '%m/%d/%y %H:%M:%S',     # '10/25/06 14:30:59'
    '%m/%d/%y %H:%M',        # '10/25/06 14:30'
    '%m/%d/%y',              # '10/25/06'
    

    So your value of 2010-12-19 17:08:22.498000 won’t work.

    The default widget for DateTimeField is DateTimeInput widget which formats the datetime to a string correctly, but HiddenInput just takes the datetime object w/ no formatting as you’ve shown.

    If you want to use HiddenInput you need to strftime the datetime object to be in the correct format.

    An alternative option if you just want to hide it is to not set the widget as HiddenInput but simply keep the DateTimeInput widget as is and hide that element with the attrs argument.

    class PostForm(ModelForm):
        class Meta:
            model = Post
            fields = ('title', 'message', 'pub_date',)
            widgets = {
                'message' : Textarea(attrs={'cols':80, 'rows':20}),
                'pub_date' : DateTimeInput(attrs={'style': 'display:none;'}),
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have just tried to save a simple *.rtf file with some websites and
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a French site that I want to parse, but am running into

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.