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 7742689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T09:20:21+00:00 2026-06-01T09:20:21+00:00

While writing some views to respond to ajax requests i find it somewhat strange

  • 0

While writing some views to respond to ajax requests i find it somewhat strange that the login_required decorator always returns a 302 status code for not authenticated users. As these views are ajax views, this seems somewhat inappropriate. I do not want the user to log in in such a case, but i want Django to tell the client that authentication is required to access such a view (a 401 should be the right status code, i think).

To achieve this, i started to write my own decorator login_required_ajax, but somehow this is beyond my skills. This is what i have come up with so far:

def login_required_ajax(function=None,redirect_field_name=None):
    """
    Just make sure the user is authenticated to access a certain ajax view

    Otherwise return a HttpResponse 401 - authentication required
    instead of the 302 redirect of the original Django decorator
    """
    def _decorator(view_func):
        def _wrapped_view(request, *args, **kwargs):
            if request.user.is_authenticated():
                return view_func(request, *args, **kwargs)
            else:
                return HttpResponse(status=401)

        if function is None:
            return _decorator
        else:
            return _decorator(function)

When using this decorator on a view, i get a ViewDoesNotExist exception as soon as i try to access any page on the site.

I first thought that the problem could be the direct return of an HttpResponse when a user is not authenticated, because a response object is not a callable. But then the decorator should work as long as i do not try to access the view in question, shouldn’t it? And if this really is the crux, how can i write a decorator that returns a HttpResponse with a status code of 401?

  • 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-06-01T09:20:23+00:00Added an answer on June 1, 2026 at 9:20 am

    That’s a pretty good attempt. Here’s a couple of problems I spotted:

    1. Your _decorator function should return _wrapped_view.
    2. The indentation for your if function is None block is a bit off — the login_required_ajax function needs to return the decorated function.

    Here’s the decorator with those changes made:

    def login_required_ajax(function=None,redirect_field_name=None):
        """
        Just make sure the user is authenticated to access a certain ajax view
    
        Otherwise return a HttpResponse 401 - authentication required
        instead of the 302 redirect of the original Django decorator
        """
        def _decorator(view_func):
            def _wrapped_view(request, *args, **kwargs):
                if request.user.is_authenticated():
                    return view_func(request, *args, **kwargs)
                else:
                    return HttpResponse(status=401)
            return _wrapped_view
    
        if function is None:
            return _decorator
        else:
            return _decorator(function)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing some code that looks like this: while(true) { switch(msg->state) { case MSGTYPE:
While making some final tests of a class-library that I'm writing for Windows Mobile
For some of my Django views I've created a decorator that performs Basic HTTP
While writing some C code, I decided to compile it to assembly and read
I have come across an annoying problem while writing some PHP4 code. I renamed
I have some troubles while writing logs from log4net to the file. I seem
A while ago, I was writing some code, but accidentally deleted some good code
While writing code in a file that would comprise of PHP, HTML, CSS &
While writing a card shuffling algorithm I realized that there are 52! ~= 2^225
I’m writing an objective-c program that does some calculations based on time and will

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.