Reputation: 41
I have a modelForm on a page which is submitted, hitting the below view:
def add_review(request, id=None):
if request.method == 'POST':
review_form = ReviewForm(request.POST or None)
if review_form.is_valid():
# do some stuff here...
return redirect('/movies/{0}'.format(id))
Currently redirects back to the page which we came from, this causes any form errors to be lost. So when on said page the user gets no error message output to advise them.
Is there a way I can either pass along any review_form.errors to the view being redirected to, or just return that view instead of redirecting to it? Here's that view:
def view(request, id=None):
movie = get_object_or_404(Movie, id=id)
context = Context({
'movie': movie,
'review_form': ReviewForm(),
'reviews': Review.objects.filter(
movie=movie).order_by('-created_on')[:3],
'genres': movie.moviegenre_set.filter(movie.title),
})
return render(request, 'movies/view.html', context)
Thanks
Upvotes: 1
Views: 150
Reputation: 10811
Looking at the doc
While it is possible to process form submissions just using Django’s HttpRequest class, using the form library takes care of a number of common form-related tasks. Using it, you can:
- Display an HTML form with automatically generated form widgets.
Check submitted data against a set of validation rules.
3. Redisplay a form in the case of validation errors.
- Convert submitted form data to the relevant Python data types.
To display the current form's errors in the html you only have to do this
{{ form.field.errors }}
Upvotes: 1