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

The Archive Base Latest Questions

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

I am using jQuery to do an AJAX POST, but get a CSRF error.

  • 0

I am using jQuery to do an AJAX POST, but get a CSRF error. I assume this is because the CSRF cookie isn’t sent to the client to send back. Is there a way I can fix this in Django 1.0?

  • 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-01T00:00:03+00:00Added an answer on June 1, 2026 at 12:00 am

    Yuji’s suggestion pointed me in the right direction.

    I was doing the AJAX POST from a view which was a result of a GET so the Django middleware did not generate any csrf token.

    I used the code found in the csrf middleware (/usr/lib/python2.5/site-packages/django/contrib/csrf/middleware.py) to generate a csrf token which I send with the GET context. Now the AJAX POST works fine since the token is present.

    To create the csrf token:

    def csrf_token_create(request):
    
        from django.contrib.csrf.middleware import _make_token    
    
        try:
            session_id = request.COOKIES[settings.SESSION_COOKIE_NAME]
        except KeyError:
            # No session, no check required
            return None
    
        csrf_token = _make_token(session_id)
        return csrf_token
    

    In the view context:

    csrf_token = csrf_token_create(request)
    return render_to_response('view.html', {'csrfmiddlewaretoken': csrf_token})
    

    Add a hidden input for csrfmiddlewaretoken in the html template.

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

Sidebar

Related Questions

How to pass multiple checkboxes using jQuery ajax post this is the ajax function
I want to call a webservice using jQuery.ajax() but the webervice doesn't get called.
I've been trying to get this really simple example of using AJAX with JQuery
I'm using jquery ajax to post updates back to my server. I'm concerned about
I am posting to the server using jquery ajax using $.post. After server returns
I'm stumped. I'm using jquery and ajax to POST some fields from a form
I am using JQuery to post with AJAX to another ASP page. Do I
I'm using jQuery to make an Ajax call using an Http Post in ASP.NET
I have a spring 3 application where i am using jquery to post ajax
I'm using ajax in wordpress to get the output of a function: jQuery.ajax({ type:

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.