Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I specify form validation errors when they occur?
    primarykey
    data
    text
    <p>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 <em>Animal</em> <code>Model</code> and a <code>ModelForm</code>:</p> <pre><code>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 </code></pre> <p>I render this info via urls.py, calling this view:</p> <pre><code>@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) </code></pre> <p>It seems that I have an error introducing the <code>adoption_limit</code> 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.</p> <p>I would like to know how can I do two things:</p> <ol> <li>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.</li> <li>How can I put the same input type for <code>DateTimeField</code> that I have in the admin interface? (with the Today and Now functions)</li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload