python - Problems with Django Hitcount Module -
i'm trying use django-hitcount module save number of times tutorial accessed different users.
the module correctly installed explained in blog. hitcount template tag seems return correct result.
<script type="text/javascript"> $(document).ready(function() { {% get_hit_count_javascript tut_contents %} }); </script> <script type="text/javascript"> // returns $(document).ready(function() { $.post( '/tutorial/ajax/hit/', { hitcount_pk : '1' }, function(data, status) { if (data.status == 'error') { // error? } }, 'json'); }); </script>
my urls.py
from django.conf.urls import patterns hitcount.views import update_hit_count_ajax django.conf.urls import url urlpatterns = patterns('tutorial.views', (r'^$', 'root'), # hitcount url save hits on tutorial entity url(r'^ajax/hit/$', update_hit_count_ajax, name='hitcount_update_ajax'), )
the problem when check debugger.
failed load resource: server responded status of 403 (forbidden) post http://waaave.com.dev:8000/tutorial/ajax/hit/ 403 (forbidden)
(that's why nothing saved in hitcount_hit table)
since sending request post , getting 403 forbidden, i'm going guess problem csrf token, in django required in every post request.
solving easy. copy getcookie function from documentation , either send header explain, or add data in request this:
$(document).ready(function() { $.post( '/tutorial/ajax/hit/', { 'hitcount_pk' : '1', 'csrfmiddlewaretoken': getcookie('csrftoken') }, function(data, status) { if (data.status == 'error') { // error? } }, 'json'); });
Comments
Post a Comment