Questions

Redirect to the page from where login is request in django

0

I want to redirect to the page from where login was requested.



asked 1 year, 10 months ago
Reputation: 1





1 Answer

0

You can do this way:

login function:

from .forms import LoginForm
from django.contrib.auth import login

def login(request):
    next_url = request.POST.get('next', request.GET.get('next', '/'))
    form = LoginForm(request.POST)
    if request.method == 'POST':
        if form.is_valid():
            login(request, form.user)
            return redirect(next_url)
    else:
        form = LoginForm()
    return render(request,'users/login.html', {'form': form, 'next': next_url})

login.html templae:

{% extends "base.html" %}

{% block content %}
    <form method="post" action="{% url 'login' %}">
        {% csrf_token %}
        <table>{{ form.as_table }}</table>
        <input type="submit" value="login" />
        {% if next %}
        <input type="hidden" name="next" value="{{ next }}" />
        {% endif %}
    </form>
    <a href="{% url 'register' %}">Or sign up</a>
{% endblock %}

answered 1 year, 10 months ago
Reputation: 1





Your Answer

Nothing to preview

Post Answer



Asked:  1 year, 10 months ago
Viewed:  526 times
Active:  1 year, 10 months ago