Home>

I'm learning django.
I want to count the number of clicks to the URL on the page that puts the title and URL of the news article on the net as a model in mysql and displays it as a list.
models.py

class News (models.Model):
    news_title = models.TextField ()
    news_url = models.TextField ()
    view_number = models.IntegerField (blank = False, default = 0)


views.py is

def news_list (request):
    news = News.objects.order_by ('id'). reverse ()
    if request.method =='POST':
            view_number = int (request.POST ['view_number'])
            news_number = int (request.POST ['news_number'])
            viewd_news = get_object_or_404 (News, id = news_number)
            viewed_news.view_number + = view_number
            viewd_news.save ()
    return render (request,'mysite/news.html', {'news': news})


count.js

$(function () {
    // Used to get csrf_token
    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;
    }
    function csrfSafeMethod (method) {
        // these HTTP methods do not require CSRF protection
        return (/ ^ (GET | HEAD | OPTIONS | TRACE) $/.test (method));
    }
    for (let number = 1;number<= 1000;number ++) {
        $('.news_'+ number) .click (function () {
            $.ajax ({{
                type:'POST',
                url:'./news',
                data: {
                    view_number: 1,
                    news_number: number,
                },
                beforeSend: function (xhr, settings) {
                    if (! csrfSafeMethod (settings.type)&&! This.crossDomain) {
                        xhr.setRequestHeader ("X-CSRFToken", csrf_token);
                    }
                },
              });

        });
    }
});


It is said.
count.jsPost with Django ajax. Also add csrf_token.I am making it with reference to the page of.
On the page

title

I'd like to increase the value of view_number when clicked so that the number of clicks can be displayed next to the article, but the view_number does not change even if I jump from the url to the article with the above code It was.
count.js has been read and no particular error has occurred. Where should I fix it?

[Addition]
count.js

beforeSend: function (xhr, settings) {
                    if (! csrfSafeMethod (settings.type)&&! This.crossDomain) {
                        xhr.setRequestHeader ("X-CSRFToken", csrf_token);
                    }
                },


If i comment out the part and click the URL
Forbidden (CSRF token missing or incorrect.):/News
Forbidden (CSRF token missing or incorrect.):/News
[17/Nov/2020 17:09:42] "POST/news HTTP/1.1" 403 2556
[17/Nov/2020 17:09:42] "POST/news HTTP/1.1" 403 2556
I heard that the CSRF token cannot be found, but even if I insert the above code again and click it, it just jumps to the link and the POST log does not appear.

  • Answer # 1

    solved! !! There were various missing parts in count.js.