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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:19:19+00:00 2026-05-13T19:19:19+00:00

I’m writing a simple auth system to login (and logout) users. The username is

  • 0

I’m writing a simple auth system to login (and logout) users. The username is an email address, which looks up an email field.

I’m using:

user = User.objects.get(email__exact=email)
# if user obj exists
if user:
    # if authenticate
    if authenticate(user, email, password):
        # create session
        request.session['user'] = user
        # redir
        return HttpResponseRedirect('/home/')
    else:
        return HttpResponseRedirect('/home/login/')
# no user obj found? (no email found actually)
else:
    # redir
    return HttpResponseRedirect('/home/')

to find if a user exists, however if a user is not found Django throws an error:

User matching query does not exist.

All I want to do is see if that query matches a result. How do I count rows?

  • 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-13T19:19:19+00:00Added an answer on May 13, 2026 at 7:19 pm

    I think this is probably more what you want:

    try:
        user = User.objects.get(email__exact=email)
        if authenticate(user, email, password):
            request.session['user'] = user
            return HttpResponseRedirect('/home/')
        else:
            return HttpResponseRedirect('/home/login/')
    except User.DoesNotExist: # A user with the e-mail provided was not found
        return HttpResponseRedirect('/home/')
    

    Personally I think that’s cleaner than Ignacio’s method, but it’s a matter of taste really.

    Finally, the last part of your question (“How do I count rows?”):

    get only ever returns an object, and raises an exception if no objects match the criteria you passed in, or if multiple objects match the criteria. To get the number of objects in a QuerySet, you can just call len on it:

    q = User.objects.filter(email = 'foo@bar.com')
    print len(q)
    

    Or, call count():

    print User.objects.filter(email = 'foo@bar.com').count()
    

    The former method (len) will be quicker if you’ve already evaluated the QuerySet (since it’s just counting the number of items in a list, effectively); the latter (count()) will be faster if you’ve not evaluated the QuerySet, since Django will perform a SELECT COUNT(*) behind the scenes, and you avoid loading all the data into memory.

    As an aside – why are you directing them to /home/ if the e-mail address isn’t matched, and to /home/login/ if their credentials fail? I would have thought it makes more sense to redirect them to the same place for those two cases.

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

Sidebar

Related Questions

I would like to run a str_replace or preg_replace which looks for certain words
I'm making a simple page using Google Maps API 3. My first. One marker
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
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

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.