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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T01:55:34+00:00 2026-06-14T01:55:34+00:00

I have a view that is being called with ajax and returns JSON. I’d

  • 0

I have a view that is being called with ajax and returns JSON.

I’d like it to require authentication.

@login_required
def my_view(request):
    data = some_data
    return HttpResponse(json.dumps(data), mimetype='application/json')

Ideally, if I’m not authenticated this view should return a HTTP status 401 Unauthorized or something similar, so my javascript can correctly interpret the response.

Is it a good practice for requiring authentication on Ajax requests ?
If yes, how could I get this HTTP status from the @login_required decorator ?

  • 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-14T01:55:35+00:00Added an answer on June 14, 2026 at 1:55 am
    1. whether it is a good practice or not depends on what the resource the ajax request is requesting
    2. you don’t have to use login_required decorator, you can simply check the credentials inside my_view instead. And if check fails, return 401.
    3. If you have quite a few resources and you want to build a REST API on top of it for Ajax purpose, consider using a framework such as Tastypie.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a nested form in a rails view that is called like this
I have a basic JsonResult method that is being called by a jQuery $.ajax
I have a View that is basically setup like this: <Grid> <ViewBox> <Grid> <ItemsControl
I have a view that could be called from any of 3 actions from
I have a controller that is being called twice from an ActionLink call. My
I have a function, in UpdateViewController, that is being called by a delegate, MyDownloadController,
I have a view which is not being called when I step through the
I currently have a partial view that is called within my _Layout.cshtml, and is
In MVC 3 project, I have a Controller action that returns a View. Let's
I have this ajax request which is being called whenever I change an input

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.