Skip to content
Advertisement

django using ajax to call backend has problem

I’m new to Django. I want to create liking icon for my blog post. here is a heart icon in my html file, and I want when I click it, it turns to red and then calls a function in backend to change a number in database and send back the new number to the template, all using Ajax, in order not to refreshing the page after liking the post. What should I do, and where is the problem?

In html file:

JavaScript

The script part:

JavaScript

In views:

JavaScript

In urls.py:

JavaScript

And the error is:

Internal Server Error: /notes/1/vote/like/ Traceback (most recent call last): File “/home/niloofar/git/djangoenv/lib/python3.9/site-packages/django/core/handlers/exception.py”, line 47, in inner response = get_response(request) File “/home/niloofar/git/djangoenv/lib/python3.9/site-packages/django/core/handlers/base.py”, line 181, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) TypeError: like() got an unexpected keyword argument ‘id’

Advertisement

Answer

A request to /1/vote/like/ would match the URL <int:id>/vote/like/ in the list and Django would call the function views.like(request, id=1) but your function only accept single argument which is request hence the error.

Change your function like to something like below then it should work fine.

JavaScript

OR

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement