Okay guys, I'm pretty stumped with this one!
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)
I am using django as the framework and have everything working..... almost. Following this question: Periodically fetch data and display it with Django
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.
If I had to guess, I would say that.. for some reason, $.ajax is not being called when there is not a browser.
Here is the requests.py 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)
And, the $.ajax request comes from:
{% extends "base.html" %}
{% block script %}
<script>
$(document).ready(function(){
function pullSoup(){
var token = "{{ csrf_token }}"
var soup_text = "{{ soup_text }}"
var soup_text = soup_text.replace(/&/gi, "&").replace(/"/gi, "\"").replace(/'/gi, "'").replace(/>/gi, ">").replace(/</gi, "<");
var formdata = "csrfmiddlewaretoken=" + token + "&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()
});
</script>
{% endblock script %}
{% block content %}
<p>you just pulled soup. unless you know what you're doing... you probably
should not do that again.</p>
{% endblock content %}
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:
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
And when I run the requests.py file it outputs:
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
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!