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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T21:52:27+00:00 2026-05-24T21:52:27+00:00

I am quite new to Django and jquery stuff. I am trying to populate

  • 0

I am quite new to Django and jquery stuff. I am trying to populate a comboBox (ChoiceField in Django) based ont the choice selected in another comboBox (without reloading the page).

I can’t find any simple example of such a basic application of ajax.

For now I’m call the following ajax function when I select an item from the first dropdown list.

function get_asset_from_type(){
        var type_asset = $("#id_type").val();
        var data = {type_asset:type_asset};
        var args = {type:"POST", url:"/asset/etatType/", data:data};
        $.ajax(args);

        alert(type_asset);

        return false;
};

It alerts the right type but gives a 403 error on the given url. Weird thing is this url works the first time I load the page. I don’t understand what’s going on..

EDIT:
403 error seems to be gone, remains the initial question 🙂

  • 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-24T21:52:27+00:00Added an answer on May 24, 2026 at 9:52 pm

    I think you’re running up against a CSRF problem. As Django by default blocks POST requests that do not have a CSRF Token with a 403. There are a couple ways to deal with this in JS. One is to pull the value out of the cookie, the code to do that can be found here: https://docs.djangoproject.com/en/dev/ref/contrib/csrf/#ajax

    or you can do it by passing the CSRF_TOKEN in with the javascript script tag:

    <script src='myjavascript.js?CSRF_TOKEN={{ csrf_token }}'></script>
    

    Note that it’s using a double braket, instead of {%%}. This gets the value of the token, instead of the form input.

    function getOptionsFromScriptSrc() {
        // Get last script tag in parsed DOM.
        // Due to the way html pages are parsed, 
        // the last one is always the one being loaded.
    
        var options = {}
        var js_src = $('script').last().attr('src');
    
        if(js_src.match(/\?/)) {
            var options_list = js_src.split('?')[1].split('&');
            for(var i = 0; i < options_list.length; i++) {
                var tmp = options_list[i].split('=');
                options[$.trim(tmp[0])] = $.trim(tmp[1]);
            }
        }
    
        return options;
    }
    
    function get_asset_from_type(){
        var options = getOptionsFromScriptSrc();
        var type_asset = $("#id_type").val();
        var data = {type_asset: type_asset, csrfmiddlewaretoken: options['CSRF_TOKEN']};
        var args = {type:"POST", url:"/asset/etatType/", data:data};
        $.ajax(args);
    
        alert(type_asset);
    
        return false;
    };
    

    I haven’t, of course, tested this code, but I have used this method before and it works pretty well.


    To the main problem of populating a select box, you need to specify a callback for your ajax post, and then deal with the data returned from your server:

    function get_asset_from_type(){
        var options = getOptionsFromScriptSrc();
        var type_asset = $("#id_type").val();
        var post_data = {type_asset: type_asset, csrfmiddlewaretoken: options['CSRF_TOKEN']};
    
        $.post('/asset/etatType/', post_data, function(data){
            // Assuming server is going to respond with the html of the options, eg: <option value="1">One</option><option value="2">Two</option>...
            $('#id_ofmyselectbox').append(data);
        }); 
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm quite new to django, and moved to it from Drupal. In Drupal is
Quite new to maven here so let me explain first what I am trying
Im quite new to spring mvc. What I'm trying to achive is separating static
I'm quite new with Django, and so far I have a pretty basic setup
I am quite new to Python and Django, and totally new on Stack Overflow,
I am new to Django and I am trying to build a data hierarchy
I'm quite new to Django & Tastypie. I would like to return only one
I'm new to Macs (and quite new to Django) and I'm setting up an
I am totally new to jQuery. I spent quite a bit of time researching
I'm quite new to Django and I'm working on a project with i18n, the

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.