Return to the requested page after successfull @login_required by the next variable

39 views
Skip to first unread message

Robin Lery

unread,
Jan 9, 2015, 12:02:50 AM1/9/15
to django...@googlegroups.com
Suppose this is the url after `@login_required(login_url='/accounts/required_login/')`:

http://ngoksy.com/accounts/login_required/?next=/article/

view for the login_require:

    def required_login(request):
        return render(request, 'required_login.html')


I tried adding 'next', like it [here][1]:

    def required_login(request):
        c = {'next' : request.GET.get('next', '/')}
        return render(request, 'login_required.html', c)


But it didn't help.

I have different template for login and another template for anonymous user which is redirected by `@login_required`.

required_login.html:

   <h2>Login required! You must login to view the content.</h2>

        {% if form.error %}
            <p class="error">Sorry, invalid username/password!</p>
        {% endif %}

        <form action="/accounts/auth/" method="post">
            {%csrf_token%}

            <label for="username">Username:</label>
            <input type="text" name="username" id="username" value="">
            <label for="password">Password:</label>
            <input type="password" name="password" id="password" value="">

            <input type="submit" value="LOGIN">
        </form>


I want to pass the `next` variable after successful login of `login_required()`, and land into Articles page after that. How do I do that? Please help me understand. Your help will be greatly appreciated. Thank you!

Vijay Khemlani

unread,
Jan 9, 2015, 7:22:29 AM1/9/15
to django...@googlegroups.com
Add the "next" variable as part of the form in a hidden field

<form....>

<input type="hidden" name="next" value="{{ next }}" />

... other fields...

</form>

and then you can get it in the view that handles the login 

Also, you seem to have a form object, why don't you use "form.as_p" or something like that to render the form?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2B4-nGqds1Ex9Rrw9temyN2D7utJ0FkF7b_sTcOyO7strxt2nw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages