Back

Explore Courses Blog Tutorials Interview Questions
+1 vote
3 views
in Web Technology by (19.9k points)

I'm Trying to pass data from javascript on a page to another view in my django project.But it seems the view isn't getting called. What I want to do is that Using JS I want to just get data from page containing the JS and print it from django view to console.

js on html page:

    <script>

    var URL="{% url 'projects:Unread' %}"

    function Unread(){

            var data = {'count': '5', 'X-csrfmiddlewaretoken': csrfmiddlewaretoken};

            $.post(URL, data);

        }

       </script>

somewhere from page I'm calling this function,I have used an alert to make sure it is called.

my urls.py :

    path('notification/',Unread,name='Unread')

my view:

def Unread(request):

  count=request.body['count']

  print("hello")

  print("count status ",count)

I have also tried request.POST to get value but now working.What I exactly want is to get 'count' from JS to my view.But its not happening, Even the 'hello' is not getting printed it's not even related to JS.I'm fairly new to both django and JS so I'm not sure what I'm doing wrong.

1 Answer

0 votes
by (25.1k points)

As the commenters mentioned, you should double check to make sure your POST isn't failing for CSRF reasons. If you're certain it is not, you can access form-encoded post variables using request.POST, don't use request.body.

def Unread(request):

    count=request.POST['count']

    print("hello")

    print("count status ",count)

Related questions

Browse Categories

...