I am currently working on a modal login box which submits and processes the login request without reloading the page. After the successful login, there are two possible outcomes:
-
the user wanted to follow a link beforehand which is only accessible for logged in users: the user gets redirected to that page after the login
-
the user clicked on an element which triggers an AJAX call which is only accessible for logged in users: the ajax call gets fired once more after the login
The template
{% load url from future %}
<form action="{% url 'login' %}" method="post">{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Login" />
</form>
Now, i am not sure how to do this. My main problem is to determine which URL needs to be processed after the login. How can i know which URL is currently in the queue for processing after a succesful login?
When you get redirected to the login-page, usually a next parameter is passed with the request. This is true for ajax-requests as well. You just have to add a hidden input field to your form to pass the “next” parameter with the url the not-logged user tried to get:
The standard login view would then return a HttpResponseRedirect, which, for your ajax-like login, is not what you would want. So you will have to write the login-view yourself and override that behaviour.