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
Differencebetween _Init_ and _Call_
How to Reversibly Store and Load a Pandas Dataframe To/From Disk
How to Make Abstract Classes in Python
How to Change the Name of a Django App
Proper Indentation for Multiline Strings
Plotly: How to Define Colors in a Figure Using Plotly Graph Objects and Plotly Express
Don't Wait for a Page to Load Using Selenium in Python
Django: Multiple Models in One Template Using Forms
Using a Pre-Trained Word Embedding (Word2Vec or Glove) in Tensorflow
How to Apply a Disc Shaped Mask to a Numpy Array
Check If Any Alert Exists Using Selenium with Python
How to Use the Ellipsis Slicing Syntax in Python
Convert Tuple to List and Back
Nested Ssh Using Python Paramiko
Matplotlib: Annotating a 3D Scatter Plot
How to Take Partial Screenshot with Selenium Webdriver in Python