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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:30:31+00:00 2026-06-10T02:30:31+00:00

I have a very vague question that I’m not sure where to start from.

  • 0

I have a very vague question that I’m not sure where to start from.

I need to make an ajax login using python (django REST calls)

jQuery is available from my end.

I was given this:

    import sys, base64
    import httplib, urllib, urlparse, json
    auth = 'Basic %s' % base64.encodestring('%s:%s' % (username, password)).strip()
    headers = {'Content-Type':'application/x-www-form-urlencoded', 'Authorization':auth}
    endpoint = "urlendpoint.com"
    url = "/login/"
    conn = httplib.HTTPSConnection(endpoint)
    conn.request("POST", url, "", headers)
    response = conn.getresponse()
    if response.status != httplib.OK: raise Exception("%s %s" % (response.status, response.reason))
    result = json.load(response)
    if "token" not in result: raise Exception("Unable to acquire token.")
    if "sessionid" not in result: raise Exception("Unable to acquire session ID.")
    print result["token"], result["sessionid"]
    conn.close()

I need to send the login via POST and then set a cookie.

I have absolutely no clue where to begin with this task. Using the command line, I’m able to set up a /login.py file, access said file with a username and password hard-coded in the above variable field and VIOLA – it works fine. However, I have no clue where to begin using ajax for this task.

The main thing here is that I need to establish a session ID between the logged in user and the server once the person has logged in, in order to get access to the REST (json) endpoints so that I can start adding data (via Ajax).

Any assistance would be much appreciated.

I’m hoping that someone

  • 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-10T02:30:33+00:00Added an answer on June 10, 2026 at 2:30 am

    There’s functionally no difference between how it’s done for a regular view and how you’d do it for an AJAX view; the only difference is what response you send:

    from django.contrib.auth import authenticate, login
    from django.http import HttpResponse, HttpResponseBadRequest
    from django.utils import simplejson
    
    def ajax_login(request):
        if request.method == 'POST':
            username = request.POST.get('username', '').strip()
            password = request.POST.get('password', '').strip()
            if username and password:
                # Test username/password combination
                user = authenticate(username=username, password=password)
                # Found a match
                if user is not None:
                    # User is active
                    if user.is_active:
                        # Officially log the user in
                        login(self.request, user)
                        data = {'success': True}
                    else:
                        data = {'success': False, 'error': 'User is not active'}
                else:
                    data = {'success': False, 'error': 'Wrong username and/or password'}
    
                return HttpResponse(simplejson.dumps(data), mimetype='application/json')
    
        # Request method is not POST or one of username or password is missing
        return HttpResponseBadRequest()        
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a very vague question, but I am not sure where to start,
My $.ajax() is not serializing the button name and value. I have a very
Even before telling my question, will tell you that this is a very vague
I have very very big html page/data. I need to fetch data under h1
Forgive me if this question is very vague Some time back i faced this
I know this is a very vague question, but I'm looking for a very
I asked this question earlier but I was very vague and a lot of
I have an assignment from my programming class, which is very poorly worded... The
This might be a very vague question but I guess I don't really understand
I have a slightly vague question. I have the following in my code: http://jsfiddle.net/PMnmw/2/

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.