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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:56:17+00:00 2026-05-24T13:56:17+00:00

Question Clarification: I’m trying to test if the user is authenticated or not for

  • 0

Question Clarification:
I’m trying to test if the user is authenticated or not for each page request.

I’m trying to use Authentication for the first time in Django and I am not grasping how the login view is supposed to handle authentications.

When I use @login_required, I’m redirecting to “/login” to check if the user is logged in and if not, display the login page. However, trying to redirect back to the original page is causing an infinite loop because it’s sending me back to the login page over and over again.

I’m clearly not grasping how @login_required is supposed to work but I’m not sure what I’m missing. I’ve been searching around for awhile for an example, but everyone uses the default @login_required without the ‘login_url’ parameter.

So for example.. the page I’m trying to access would be…

@login_required(login_url='/login')
def index(request):

And then my login would be.. (obviously incomplete)..

Edit: just to note.. the session variables are set in another view

def login(request):
    if '_auth_user_id' in request.session:

        # just for testing purposes.. to make sure the id is being set
        print "id:",request.session['_auth_user_id']

        try:
            user = Users.objects.get(id=request.session['_auth_user_id'])
        except:
            raise Exception("Invalid UserID")

        # TODO: Check the backend session key

        # this is what I'm having trouble with.. I'm not sure how
        # to redirect back to the proper view
        return redirect('/')
    else:
        form = LoginForm()

        return render_to_response('login.html',
            {'form':form},
            context_instance=RequestContext(request)
        )
  • 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-24T13:56:18+00:00Added an answer on May 24, 2026 at 1:56 pm

    Well, as you say, obviously that’s not going to work, because it’s incomplete. So, until you complete it, you’re going to get an infinite loop – because you haven’t written the code that puts _auth_user_id into request.session.

    But I don’t really know why you’re making that test in the first place. The auth documentation has a perfectly good example of how to write a login view: get the username and password from the form, send them to authenticate to get the user object, then pass that to login… done.

    Edit I think I might see where your confusion is. The login_required decorator itself does the check for whether the user is logged in – that’s exactly what it’s for. There’s no need for you to write any code to do that. Your job is to write the code that actually logs the user in, by calling authenticate and login.

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

Sidebar

Related Questions

Clarification: this question is not about access modifier Confirmed that B.m() and b.m() statements
Clarification/summary for the question -- we're looking for: a hosted bug tracking system, that
Question says it all, really. My application is a time tracker. It's currently written
Clarification question as a follow up to: What exactly must I do in viewDidUnload?
Im doing this question and some clarification would be super helpful. What exactly would
Bounty clarification I know it's a subjective question. The ideal answer I'm looking is
Quick question/clarification required here. I have a DB table that will quite possibly have
Simple question, I'm just looking for a yes or no clarification. A citation would
Broadly, how does inter-process and inter-thread communication work? Clarification: I understand this question can't
This is a clarification question: I'm studying for MCTS 70-515 and in my training

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.