HTTP ERROR 405
HelloGit100 opened this issue · 4 comments
Django 2.2.1
The steps are as follows:
-
pip3 install pinax-likes
-
Added
'pinax.likes',
to the project's settings.py file,
then added:
PINAX_LIKES_LIKABLE_MODELS = {
"app.Post": {}
}
- Added
re_path(r'^likes/', include(('pinax.likes.urls', 'pinax_likes'), namespace='pinax_likes')),
to the project's urls.py file,
- Download the files in the
https://github.com/pinax/pinax-templates/tree/master/pinax/templates/templates/pinax/likes
,
put these html files in /home/www/venv/templates/pinax/likes/
,
- Added
{% load pinax_likes_tags %}
{% likes_widget request.user post %}
to the post.html file,
-
python manage.py makemigrations app && python manage.py migrate && python manage.py runserver 0.0.0.0:8000
-
Open http://127.0.0.1:8000/post/13/, like icon appears, when clicked the icon(url: http://127.0.0.1:8000/like/14:13/), show the following:
HTTP ERROR 405
,
The console displays the following information:
Method Not Allowed (GET): /likes/like/14:13/
Method Not Allowed: /likes/like/14:13/
[29/May/2019 10:00:00] "GET /likes/like/14:13/ HTTP/1.1" 405 0
- Then i added
<script src="{% static 'eldarion-ajax.min.js' %}"></script>
in base.html,
After clicked the icon, the console displays the following information:
Forbidden (CSRF token missing or incorrect.): /likes/like/14:13/
[29/May/2019 10:01:31] "POST /likes/like/14:13/ HTTP/1.1" 403 2513
What are the steps wrong? Thank you!
Not sure answer atm, but looks you had similar problem as @agiledesign2.
@HelloGit100 Did you end up finding a solution, or unresolved?
Hello @agiledesign2 has posted a solution to his issue. Perhaps, this would solve your problem as well: #57
I added this to the html header to solve issue for me
<script src="{% static "js/jquery.min.js" %}" ></script>
<script>
$.ajaxSetup({
beforeSend: function(xhr, settings) {
function getCookie(name) {
var cookieValue = null;
if (document.cookie && document.cookie != '') {
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
var cookie = jQuery.trim(cookies[i]);
// Does this cookie string begin with the name we want?
if (cookie.substring(0, name.length + 1) == (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
}
}
return cookieValue;
}
if (!(/^http:./.test(settings.url) || /^https:./.test(settings.url))) {
// Only send the token to relative URLs i.e. locally.
xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));
}
}
});
</script>