I have a simple code where I make a POST
call to URL in Django
with CSRF token included. When I paste the javascript
code in the template within <script>
tags, it works fine, however as soon as I move the code to a static file, I get HTTP 403 response. This is strange!
Here is the code:
$(document).ready(function() {
$(".like-button").click(function(event) {
$.ajax({
url: <relative path of URL>,
type: "POST",
data: {
csrfmiddlewaretoken: "{{ csrf_token }}",
// other data items
},
success: function(data, textStatus, jqXHR) {},
error: function(jqXHR, textStatus, errorThrown) {}
});
// other javascript code to toggle like button class
});
});
Here's how I include the static file in Django
template:
{% block javascript %}
{% load static %}
<script type="text/javascript" src="{% static 'app/app.js' %}"></script>
{% endblock %}
The file is located in static directory:
app
├── static
│ └── app
│ └── app.js
and static settings are:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
Note, I do not have STATICFILE_DIRS
in the settings yet. I can verify that the file is being loaded and executed, since console.log()
statements work perfectly fine. However, I get HTTP 403 request upon POST
.