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

  • Home
  • SEARCH
  • 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 579919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:25:02+00:00 2026-05-13T14:25:02+00:00

I’m working on a large Django app, the vast majority of which requires a

  • 0

I’m working on a large Django app, the vast majority of which requires a login to access. This means that all throughout our app we’ve sprinkled:

@login_required
def view(...):

That’s fine, and it works great as long as we remember to add it everywhere! Sadly sometimes we forget, and the failure often isn’t terribly evident. If the only link to a view is on a @login_required page then you’re not likely to notice that you can actually reach that view without logging in. But the bad guys might notice, which is a problem.

My idea was to reverse the system. Instead of having to type @login_required everywhere, instead I’d have something like:

@public
def public_view(...):

Just for the public stuff. I tried to implement this with some middleware and I couldn’t seem to get it to work. Everything I tried interacted badly with other middleware we’re using, I think. Next up I tried writing something to traverse the URL patterns to check that everything that’s not @public was marked @login_required – at least then we’d get a quick error if we forgot something. But then I couldn’t figure out how to tell if @login_required had been applied to a view…

So, what’s the right way to do this? Thanks for the help!

  • 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-13T14:25:02+00:00Added an answer on May 13, 2026 at 2:25 pm

    Middleware may be your best bet. I’ve used this piece of code in the past, modified from a snippet found elsewhere:

    import re
    
    from django.conf import settings
    from django.contrib.auth.decorators import login_required
    
    
    class RequireLoginMiddleware(object):
        """
        Middleware component that wraps the login_required decorator around
        matching URL patterns. To use, add the class to MIDDLEWARE_CLASSES and
        define LOGIN_REQUIRED_URLS and LOGIN_REQUIRED_URLS_EXCEPTIONS in your
        settings.py. For example:
        ------
        LOGIN_REQUIRED_URLS = (
            r'/topsecret/(.*)$',
        )
        LOGIN_REQUIRED_URLS_EXCEPTIONS = (
            r'/topsecret/login(.*)$',
            r'/topsecret/logout(.*)$',
        )
        ------
        LOGIN_REQUIRED_URLS is where you define URL patterns; each pattern must
        be a valid regex.
    
        LOGIN_REQUIRED_URLS_EXCEPTIONS is, conversely, where you explicitly
        define any exceptions (like login and logout URLs).
        """
        def __init__(self):
            self.required = tuple(re.compile(url) for url in settings.LOGIN_REQUIRED_URLS)
            self.exceptions = tuple(re.compile(url) for url in settings.LOGIN_REQUIRED_URLS_EXCEPTIONS)
    
        def process_view(self, request, view_func, view_args, view_kwargs):
            # No need to process URLs if user already logged in
            if request.user.is_authenticated():
                return None
    
            # An exception match should immediately return None
            for url in self.exceptions:
                if url.match(request.path):
                    return None
    
            # Requests matching a restricted URL pattern are returned
            # wrapped with the login_required decorator
            for url in self.required:
                if url.match(request.path):
                    return login_required(view_func)(request, *view_args, **view_kwargs)
    
            # Explicitly return None for all non-matching requests
            return None
    

    Then in settings.py, list the base URLs you want to protect:

    LOGIN_REQUIRED_URLS = (
        r'/private_stuff/(.*)$',
        r'/login_required/(.*)$',
    )
    

    As long as your site follows URL conventions for the pages requiring authentication, this model will work. If this isn’t a one-to-one fit, you may choose to modify the middleware to suit your circumstances more closely.

    What I like about this approach – besides removing the necessity of littering the codebase with @login_required decorators – is that if the authentication scheme changes, you have one place to go to make global changes.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a text area in my form which accepts all possible characters from
I know there's a lot of other questions out there that deal with this
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
Let's say I'm outputting a post title and in our database, it's Hello Y’all
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is

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.