Below there is my code. I don’t know how to redirect back after some validation errors
Urls.py:
(r'^comments/', include('django.contrib.comments.urls')),
views.py:
def show_episode(request, episode) :
episode = get_object_or_404(Episode, pk=episode)
return render_to_response('episode.html', context_instance=RequestContext(request, {
'episode' : episode,
}))
episode.html:
<div class="subsection">
{% get_comment_form for episode as form %}
<table>
<form action="{% comment_form_target %}" method="post">
{% csrf_token %}
{{ form }}
<tr>
<td colspan="2" style="text-align: center">
<input type="submit" name="submit" value="Post">
<input type="submit" name="preview" value="Preview">
</td>
</tr>
<input type="hidden" name="next" value="{% url episode episode.id %}" />
</form>
</table>
</div>
<div class="subsection">{% render_comment_list for episode %}</div>
I want to come back to this form when there will be some errors.
I try, but I have an error: ‘QueryDict’ object has no attribute ‘_meta’
Code:
def comments(request) :
if request.method == 'POST' :
form = CommentForm(request.POST)
if (form.is_valid()) :
form.save()
return HttpResponseRedirect(request.POST['next'])
return HttpResponseRedirect('/')
Do it the other way around: process the form in the same view (that is, set
<form action="", this way the user won’t lose his inputs (as he would with a redirect).You can still call the other view via this view, though. (
if request.method == 'POST')When there was no Error, send a
response.redirectto a success view.