Question

I'm using django-registration to handle login and user registration in my Django app. I'd like the user to get redirected to the url they requested after they have successfully logged on to the system (assuming they were logged off when they tried to access the url initially).

The login url looks like:

http://localhost:8000/accounts/login/?next=/#foo/bar/

However, after the login, the user always ends up at http://localhost:8000/. I've tried it with http://localhost:8000/accounts/login/?next=/abc/def/ and that worked fine, so I assume the hashtag is the problem. How can this be fixed (if at all)?

Update

I should have mentioned that it's a single page app, hence the hashtag...

Was it helpful?

Solution

I started stepping through the django-registration (actually django) code. I couldn't find anything django-registration is doing during the login, so I think it isn't even responsible for this part.

Django wraps @login_required view functions in _wrapped_view(request, *args, **kwargs) functions (django.contrib.auth.decorators) and performs an authentication test before executing the actual function. If found not logged in yet, it gets the current url as next (path = request.build_absolute_uri()) and starts the login. However, request.build_absolute_uri() doesn't even return the hashtag url. Extracting the #-appended part of the url does generally not seem to be possible.

OTHER TIPS

Sorry for the necromancer, but I solved this issue with the following:

I have a template for the login form with the following:

<form method="post" action="{% url 'login' %}">
    {% csrf_token %}
    <input type="hidden" id="hidden_next" name="next" value="{{ next }}">
    ... the actual form here ...
</form>

and (I am using JQuery) the following script at the end:

<script>$(function () { 
    $("#hidden_next").attr(
        "value", "{{ next }}" + 
            $(location).attr('hash'))
    });
</script>

Note that I am repeating the {{ next }} just in case the user disables Javascript. In a single page app this is not an issue, as a user disabling javascript cannot use the page at all.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top