What to return after a django form has failed validation?
How shall I return to my page after a django form has failed validation?
This is what I have at the moment, I return a render_to_response after it has failed with my form objects and all the other objects I need to pass back in. But The url in the browser has the value of the form action and so this seems to me as if I'm doing something wrong. I would prefer if I could somehow return the objects along with a HttpResponseRedirect but looking at the docs that is not possible.
def addReview(request,reviewId):
review = AdminReview.objects.开发者_开发知识库get(id=int(reviewId))
if request.method == 'POST':
form = UserReviewForm(request.POST)
if form.is_valid():
cd = form.cleaned_data
ur = UserReview(adminReview=review,review=cd['review'],rating=int(cd['rating']))
ur.save()
return HttpResponseRedirect(reverse('filmflux.reviews.views.review', kwargs={'reviewId':review.id,'filmSlug':review.filmSlug}))
userReviews = review.userreview_set.all()
return render_to_response('review.html', {'review':review,'form': form, 'userReviews': userReviews }, context_instance=RequestContext(request))
Sessions would be good for this, or if you use AJAX you can validate the form, then return your object or some sort of error message or whatever you want after a failure. One or the other would probably be best. You could also pass params from the POST request along as GET query params to the redirected view, or pass arguments to the redirected view:
from django.shortcuts import redirect
return redirect('some-view-name', reviewID=id)
精彩评论