Like button not recording the data [closed]

2024/11/17 15:36:22

I have implemented a like button onto my view_post page, but the like's aren't been registered. When the button is clicked the page is redirected correctly but no likes are added.

views

def get_post(request, slug):try:post = BlogPost.objects.get(slug=slug)except BlogPost.DoesNotExist: messages.error(request, 'This post does not exist.')post = Nonecomment_form = CommentForm()return render(request, 'mhpapp/view-post.html', {'post': post, 'comment_form': comment_form,})def like_post(request, slug):template_name = 'view-post.html'post = get_object_or_404(BlogPost, slug=slug)liked = Falseif post.likes.filter(id=request.user.id).exists():post.likes.remove(request.user)liked = Falseelse:post.likes.add(request.user)messages.success(request, ("Thanks for the like...:-)"))liked = Truereturn redirect('get_post', {'slug': slug,})

urls

path('<slug:slug>/', views.get_post, name='viewpost'),
path('<slug:slug>/',views.like_post, name='likepost'),

html

 <strong>{{ post.total_likes }} Likes</strong>{% if user.is_authenticated %}<form action="{% url 'likepost' post.slug %}" method="POST">{% csrf_token %}{% if request.user in post.likes.all %} <button class="btn btn-outline-secondary rounded-0 custom-button" id="like" type="sumbit" name="post-id" value="{{ post.slug }}"><i class="fa-solid fa-heart-crack"></i></button>{% else %}<button class="btn btn-outline-secondary rounded-0 custom-button" id="like" type="sumbit" name="post-id" value="{{ post.slug }}"><i class="fa-solid fa-heart"></i></button>{% endif %}</form>{% else %}{% endif %}
Answer

You cannot set same path url for two views. django will loop into all url and take first which match the url path.

So in your case, your reversed url likepost always call get_post view I think.

Test like that:

path('view/<slug:slug>/', views.get_post, name='viewpost'),
path('like/<slug:slug>/',views.like_post, name='likepost'),
https://en.xdnf.cn/q/118789.html

Related Q&A

Python, Pygame, Image manipulation: Restretch a loaded png, to be the texture for an isometric Tile

Im a 17 year old programmer, trying to program an isometric game in python, with pygame. After finishing a tile engine, working with not good looking, gimp-drawn PNGs, I wondered, if it would be possib…

TypeError: list object is not callable [duplicate]

This question already has answers here:TypeError: list object is not callable while trying to access a list(9 answers)Closed 10 years ago.I cant find the problem im facing... this is exactly what the …

Tokenise text and create more rows for each row in dataframe

I want to do this with python and pandas.Lets suppose that I have the following:file_id text 1 I am the first document. I am a nice document. 2 I am the second document. I am an even …

Is the example of the descriptor protocol in the Python 3.6 documentation incorrect?

I am new to Python and looking through its documentation I encountered the following example of the descriptor protocol that in my opinion is incorrect. .It looks like class IntField:def __get__(self, …

How to clean a string to get value_counts for words of interest by date?

I have the following data generated from a groupby(Datetime) and value_counts()Datetime 0 01/01/2020 Paul 803 2 01/02/2020 Paul 210982360967 1 …

Folium - Map doesnt appear

I try to get map through Folium but only thing I can see is marker on blank page. Id like to know where is problem lies, in explorer or coding. map.py import foliummap = folium.Map(location = [46.20, 6…

python tkinter exe built with cx_Freeze for windows wont show GUI

PROBLEM SOLVED. the issue was with jaraco module, that i used for clipboard manipulation, i used pyperclip instead.I made a python app with tkinter that works fine, but I wanted to make an exe from it …

lxml tree connection and properties

I have a .dtsx file so, I have multiple components with connections, so I need to extract component that have especific connection, but I can not handle that, example: <components><component r…

Python recursive function call with if statement

I have a question regarding function-calls using if-statements and recursion. I am a bit confused because python seems to jump into the if statements block even if my function returns "False"…

How can I list all 1st row values in an Excel spreadsheet using OpenPyXL?

Using the OpenPyXL module with Python 3.5, I was able to figure out how many columns there are in a spreadsheet with:In [1]: sheet.max_column Out [1]: 4Then I was able to list the values in each of the…