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:
- 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.
- How can I put the same input type for
DateTimeFieldthat I have in the admin interface? (with the Today and Now functions)
The way you have written your view, to display form errors, in your
web/error.htmltemplate, simply output the errors:However, you don’t have explicitly pass the errors list, it is part of the form itself. A bit of simplification:
Then, in your template:
For the second part of your question – to display the django date time widget – things get a bit more involved:
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: