tango_with_django_2
tango_with_django_2 copied to clipboard
jQuery category like button doesn't work as expected
On Django 2.2 this doesn't work as expected:
“Don’t forget to add in the URL mapping, into rango/urls.py. Update the urlpatterns by adding in:
path('like$', views.like_category, name='like_category'),”
I read the docs and tried this:
re_path(r'^like/$', views.like_category, name="like_category"),
and it kind of works, but after the first like, every time I refresh once liked category, refresh automatically adds +1 to the number of likes.
In the console it's visible that opening/refreshing the category is requesting 'like_category' view at the same time:
[12/Jul/2019 19:39:02] "GET /rango/like/?category_id=5 HTTP/1.1" 200 2 [12/Jul/2019 19:39:02] "GET /rango/category/python/ HTTP/1.1" 200 6311
Hi Piotr,
Sounds strange. However, this is over the old version -- I fixed several things that were outstanding and it looks like this is one of them. The code above is a hangover from Django 1.9/1.10/1.11, where it uses regex to map patterns. So you are technically correct in trying re_path()
to fix this issue.
However, I've updated the book since you reported this to use the following:
path('like_category/', views.LikeCategoryView.as_view(), name='like_category'),
Instructions have been updated to reflect this change in URL/name.
Not sure I can replicate the behaviour regarding multiple likes coming down on it when the page is refreshed? That is very strange. Again, code has been revised -- if you want to check our model solution on the new code GitHub, would you agree with what I have put in?
Cheers, David