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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:35:57+00:00 2026-05-27T09:35:57+00:00

I’m quite new to Django Forms, and I’m facing a problem that I cannot

  • 0

I’m quite new to Django Forms, and I’m facing a problem that I cannot solve. I’ve been googling and reading the docs, but I can’t find the place where this is explained. My problem is that I have an Animal Model and a ModelForm:

class Animal(models.Model):
    name = models.CharField(max_length=300)
    age = models.PositiveSmallIntegerField()
    race = models.ForeignKey(Race)
    description = models.TextField()
    state = models.ForeignKey(State)
    pub_date = models.DateTimeField(auto_now_add=True)
    adoption_limit = models.DateTimeField(blank=True, null=True)
    location = models.ForeignKey(Location)
    publisher = models.ForeignKey(User)
    def __unicode__(self):
        return self.name

class AnimalForm(ModelForm):
    class Meta:
        model = Animal

I render this info via urls.py, calling this view:

@login_required
def new_animal(request):
    if request.method == "POST":
        form = AnimalForm(request.POST)
        if form.is_valid():
            form.save()
            return render_to_response('/')
        else:
            variables = RequestContext(request, {'e': form.errors})
            return render_to_response('web/error.html', variables)
    else:
        form = AnimalForm()
    variables = RequestContext(request, {'form': form})
    return render_to_response('web/animal_form.html', variables)

It seems that I have an error introducing the adoption_limit field, so the data does not get saved in DB. This is because I just set a date and not a time into the text field displayed by the form.

I would like to know how can I do two things:

  1. How can I send the error message to the form again, so that I can add a text next to the field that I have not set correctly? I.e., like the admin does.
  2. How can I put the same input type for DateTimeField that I have in the admin interface? (with the Today and Now functions)
  • 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-27T09:35:57+00:00Added an answer on May 27, 2026 at 9:35 am

    The way you have written your view, to display form errors, in your web/error.html template, simply output the errors:

    {%if e %}
      You had some errors in your submission!<br />
      {{ e }}
    {% endif %}
    

    However, you don’t have explicitly pass the errors list, it is part of the form itself. A bit of simplification:

    variables = RequestContext(request, {'form': form})
    return render_to_response('web/error.html', variables)
    

    Then, in your template:

    {% if form.errors %}
       You have some errors!<br />
       {{ form.errors }}
    {% endif %}
    

    For the second part of your question – to display the django date time widget – things get a bit more involved:

    # First, you need to import the widget:
    from django.contrib.admin.widgets import AdminSplitDateTime
    from django.forms import TextField
    
    # In your form class, you have to specify the widget
    # for the field.
    
    class AnimalForm(forms.ModelForm):
       pub_date = models.TextField(widget=AdminSplitDateTime)
       class Meta:
          model = Animal
    

    In order for this to work though, you have to make sure your admin media directory is accessible from your project (since all the javascript and css is included there). You’ll also to have make sure that all the stylesheets are also added. It is much easier (and simpler) to use your own javascript form widget from your preferred library.

    Finally, as stated in the documentation, if you override any fields, you need to add all the other validation logic yourself:

    If you explicitly instantiate a form field like this, Django assumes
    that you want to completely define its behavior; therefore, default
    attributes (such as max_length or required) are not drawn from the
    corresponding model. If you want to maintain the behavior specified in
    the model, you must set the relevant arguments explicitly when
    declaring the form field.

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

Sidebar

Related Questions

Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
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 have a jquery bug and I've been looking for hours now, I can't
I've got a string that has curly quotes in it. I'd like to replace
I am currently running into a problem where an element is coming back from
I want use html5's new tag to play a wav file (currently only supported

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.