Django: Redirect to Previous Page After Login

Django: Redirect to previous page after login

You do not need to make an extra view for this, the functionality is already built in.

First each page with a login link needs to know the current path, and the easiest way is to add the request context preprosessor to settings.py (the 4 first are default), then the request object will be available in each request:

settings.py:

TEMPLATE_CONTEXT_PROCESSORS = (
"django.core.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
"django.core.context_processors.media",
"django.core.context_processors.request",
)

Then add in the template you want the Login link:

base.html:

<a href="{% url django.contrib.auth.views.login %}?next={{request.path}}">Login</a>

This will add a GET argument to the login page that points back to the current page.

The login template can then be as simple as this:

registration/login.html:

{% block content %}
<form method="post" action="">
{{form.as_p}}
<input type="submit" value="Login">
</form>
{% endblock %}

How to redirect to previous page in Django after POST request

You can add a next field to your form, and set it to request.path. After you processed your form you can redirect to the value of this path.

template.html

<form method="POST">
{% csrf_token %}
{{ form }}
<input type="hidden" name="next" value="{{ request.path }}">
<button type="submit">Let's Go</button>
</form>

views.py

next = request.POST.get('next', '/')
return HttpResponseRedirect(next)

This is roughly what django.contrib.auth does for the login form if I remember well.

If you pass through an intermediate page, you can pass the 'next' value via the querystring:

some_page.html

<a href="{% url 'your_form_view' %}?next={{ request.path|urlencode }}">Go to my form!</a>

template.html

<form method="POST">
{% csrf_token %}
{{ form }}
<input type="hidden" name="next" value="{{ request.GET.next }}">
<button type="submit">Let's Go</button>
</form>

In Django, after login redirect the user to the previous page

You should be more defensive in your code to check for a value before sending it as the redirect location.

So assuming that you have a URL path named dashboard based on your code;

path('accounts/dashboard/', Dashboard.as_view(), name='dashboard'),
from django.urls import reverse

def login(request):
if request.method == 'POST':
...
if user is not None:
auth.login(request, user)
next_param = request.POST.get('next')
if next_param:
url = next_param
else:
url = reverse('dashboard')

return redirect(url)

how to return redirect to previous page in Django after POST request

First:

def login(request):
if request.method == 'GET':
request.session['login_from'] = request.META.get('HTTP_REFERER', '/')

Second:

if request.method == 'POST':
#TODO:
#Redirect to previous url
return HttpResponseRedirect(request.session['login_from'])

How do i redirect back to the login page after 10 seconds of inactivity using django?

The problem is about your login url :

Django try to redirect on http://localhost:8000/accounts/login/ but this url is not defined in any urls.py file.

Add LOGOUT_REDIRECT_URL = reverse_lazy('login_view') in the settings.py file. (don't forgot from django.urls import reverse_lazy).



Related Topics



Leave a reply



Submit