0

Supposed there is a long page with a form at the end. Now the user submits this form with invalid data and it gets rerendered.I don't want to have him scrolling all the way back down, so i want to send him to a

<a name="form"></a>

This is a related question: Django. How to redirect to url with fragment identifier The difference is, that i am rerendering instead of redirecting. Because i only redirect after success. So how do i send the user to the ancor with

@method_decorator(login_required())
def post(self, request, article_id, article):
    self.object = self.get_object() # needed to fill context
    form = CommentForm(request.POST, response_to_id=article_id, author=self.request.user)  # A form bound to the POST data
    if form.is_valid():  # All validation rules pass
        comment = form.save()
        redirect_url = reverse('article_detail', kwargs={'article_id': article_id}) + "#" + str(comment.id)
        return HttpResponseRedirect(redirect_url)  # Redirect after POST
return self.render_to_response(self.get_context_data(form=form))

the call at the end?

Community
  • 1
  • 1

1 Answers1

0

Just add the #whatever.

just add the window.location.href if form is submit,

<script>

{% if submit %}
window.location.href = "#whatever"
{% endif %}

</script>

Note: send the context variable of submit=True if request is POST.

and in your htm form,

<form id='whatever' action="some" method='post'></form>

Example: Django: How to send user to ancor after POST

Community
  • 1
  • 1
dhana
  • 6,487
  • 4
  • 40
  • 63
  • Your example link goes to the url of this thread. I don't think that's intended. –  May 23 '14 at 10:49
  • This is just link, If you click on this it will show my answer like your form submiting. – dhana May 23 '14 at 12:06
  • Well, this is embarrassing. Thank you very much for your help. It worked! –  May 23 '14 at 13:23