[Fixed]-Maintain path redirect after signup in Django

1👍

You can do this using the built-in context processors by including the request path at the end of your signup page URL.

If you have a link to the Signup page that the user is clicking on, you can include the request path like so:

<a href="{% url 'user-signup' %}?next={{ request.path }}">Signup</a> 

If you’re redirecting the user from a view, append the next parameter with the request object from the view, something like this:

from django.http import HttpResponseRedirect
from django.urls import reverse

class ClassViewThatRedirectsUser(View):
  def get(self, *args, **kwargs):
    return HttpResponseRedirect("%s%s%s" % (reverse('user-signup'), "?next=", self.request.path))

Make sure you have the context processors included in your settings file, for Django 1.10 it’s included under OPTIONS in TEMPLATES, it will look something like this:

TEMPLATES = [
{
    'OPTIONS': {
        'context_processors': [
            'django.template.context_processors.debug',
            'django.template.context_processors.request',
            'django.contrib.auth.context_processors.auth',
            'django.contrib.messages.context_processors.messages',
        ],
    },
👤bjorn

Leave a comment