title: Scheduled POST with django and ajax
body: <p>Okay guys, I'm pretty stumped with this one!</p>
  
  <p>So, I am trying to routinely post the contents of a txt file onto a website. (IE update the file daily and post the new one)</p>
  
  <p>I am using django as the framework and have everything working..... almost. Following this question:
  <a href="https://stackoverflow.com/questions/16522578/periodically-fetch-data-and-display-it-with-django">Periodically fetch data and display it with Django</a></p>
  
  <p>I set up the views, templates, and urls so that $.ajax is called whenever I load the page. And that part works! If I manually load the page into a browser, the data is updated and displayed correctly. However, automating it has been problematic.</p>
  
  <p>If I had to guess, I would say that.. for some reason, $.ajax is not being called when there is not a browser. </p>
  
  <p>Here is the requests.py code:</p>
  
  <pre><code>import requests
  login_url = "http://127.0.0.1:8000/login/"
  soup_url = "http://127.0.0.1:8000/soups/pull/"
  test_url = "http://127.0.0.1:8000/profiles/myusername"
  UN = 'myusername'
  PW = 'mypassword'
  
  with requests.session() as client:
      # get the login page
      client.get(login_url)
  
      # save session credentials
      csrftoken = client.cookies['csrftoken']
      login_data = {'username':UN,'password':PW,'csrfmiddlewaretoken': csrftoken}
  
  
      # login
      client.post(login_url, data=login_data)
      client.get(test_url)
  
      # get the page that loads $.ajax
      client.get(soup_url)
  </code></pre>
  
  <p>And, the $.ajax request comes from:</p>
  
  <pre><code>{% extends "base.html" %}
  
  
  {% block script %}
  &lt;script&gt;
  
  $(document).ready(function(){
    function pullSoup(){
        var token = "{{ csrf_token }}"
        var soup_text = "{{ soup_text }}"
        var soup_text = soup_text.replace(/&amp;amp;/gi, "&amp;").replace(/&amp;quot;/gi, "\"").replace(/&amp;apos;/gi, "'").replace(/&amp;gt;/gi, "&gt;").replace(/&amp;lt;/gi, "&lt;");
        var formdata = "csrfmiddlewaretoken=" + token + "&amp;content=" + soup_text
  
  
        $.ajax({
                    url: "/api/chirp/create/",
                    data: formdata,
                    method: "POST",
  
                    success: function(data){
                        console.log(data)
                        console.log(formdata)
                        attachChirp(data, true)
                    },
  
                    error: function(data){
                        console.log("error")
                        console.log(data.statusText)
                        console.log(data.status)
                    }
        })
  
    }
  
    pullSoup()
  
  
  
  });
  &lt;/script&gt;
  {% endblock script %}
  
  
  
  {% block content %}
  &lt;p&gt;you just pulled soup. unless you know what you're doing... you probably
      should not do that again.&lt;/p&gt;
  {% endblock content %}
  </code></pre>
  
  <p>I tried pulling the ajax request out of $(document).ready just in case that was the issue... but got the same results. When I manually load the page, the server outputs something like this:</p>
  
  <pre><code>Starting development server at http://127.0.0.1:8000/
  Quit the server with CONTROL-C.
  [28/Mar/2018 01:39:33] "GET /soups/pull/ HTTP/1.1" 200 6478
  [28/Mar/2018 01:39:35] "POST /api/chirp/create/ HTTP/1.1" 201 739
  </code></pre>
  
  <p>And when I run the requests.py file it outputs:</p>
  
  <pre><code>Starting development server at http://127.0.0.1:8000/
  Quit the server with CONTROL-C.
  [28/Mar/2018 01:41:01] "GET /login/ HTTP/1.1" 200 4887
  [28/Mar/2018 01:41:02] "POST /login/ HTTP/1.1" 302 0
  [28/Mar/2018 01:41:02] "GET / HTTP/1.1" 200 13006
  [28/Mar/2018 01:41:02] "GET /soups/pull/ HTTP/1.1" 200 6478
  </code></pre>
  
  <p>So, it has an exit code of 200 and seems like it's happy... except that the post never actually goes through! I would really appreciate any thoughts / suggestions. I'm pretty out of ideas right now. Thanks!</p>