Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7536247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T06:31:19+00:00 2026-05-30T06:31:19+00:00

We are using the @login_required decorator so that users see a login page if

  • 0

We are using the @login_required decorator so that users see a login page if they try to access a url for which they need to be authenticated.
We want to show a ‘cancel’ button on the login page, which should return the user to whichever page they were on when they tried to access the url (by clicking a link etc – we don’t need to deal with them manually entering the url).

At the moment our login.html looks for a request parameter ‘login_cancel_url’ and if present uses that (otherwise the home page).

However, this means we have to manually pass this parameter (set to the url of the current page) whenever we show a link or button that leads to an ‘authentication required’ url.

Is there a more elegant way to do this?

Thanks, Martin

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T06:31:20+00:00Added an answer on May 30, 2026 at 6:31 am

    Well you can try get the referrer header from the request but as far as I am aware, it’s browser dependent and is not very reliable so the way you are doing it is probably best. You could try make life easier by creating template tags to avoid having to rewrite the return URL manually.

    You are easily able to get the current URL from django’s request object on any page, so instead of setting it manually on the link, you could write a snippet of html:

    link_to_login.html

    <!-- You should probably get /login/ using the {% url ... %} template tag -->
    <a href="/login/?login_cancel_url={{ request.path|urlencode }}">
    Login Page</a>
    

    and use the {% include "link_to_login.html"%} template tag.

    Alternatively, If the text needs to be different depending on the link you can instead create an inclusion template tag:

    templatetags/extra_auth_tags.py

    @register.inclusion_tag('templates/extra_auth_tags/login_link.html')
    def login_link(context, text=None):
    return {
        'text':text
    }
    

    templates/extra_auth_tags/login_link.html

    <!-- You should probably get /login/ using the {% url ... %} template tag -->
    <a href="/login/?login_cancel_url={{ request.path|urlencode }}">
    {% if text %}
        {{ text }}
    {% else %}
        Some Default Text
    {% endif %}
    </a>
    

    and then call it in your templates as {% login_link text="Check you messages" %}. Be aware that keyword arguments for inclusion tags are only supported in the django dev version so you might need to write the template tag by hand.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to automate a process involving a website that is using a login
Is there any difference in using login_required decorator in urls.py and in views.py ?
Using VB.NET and SQL Server 2005 In my software I am using login page
Using VB.NET Windows Based Application In my software I am using a login page,
I'm creating a website in Ruby on Rails, where users can login using RESTful
I found a form of information leakage when using the @login_required decorator and setting
I'm using the @login_required decorator in my project since day one and it's working
By default, when using the @login_required decorator, Django performs a 302 (temporary) redirect when
So currently I'm using @login_required to block certain pages from users and redirect them,
I'm using login function in my site with session. This session of mine gets

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.