Hudy_edd
Hudy_edd

Reputation: 15

Django redirecting after clicking Like button

I'm doing Like/Unlike system in my project. After clicking Like button should just redirect on the same page but updated with +1 like but it is not. Have looked on youtube videos with this system and they got it worked but I can't figure it out what is the problem with my code.

views.py

def like_view(request, pk):
    cocktail = get_object_or_404(AddCocktails, id=request.POST.get('cocktail_id'))
    liked = False
    if cocktail.likes.filter(id=request.user.id).exists():
        cocktail.likes.remove(request.user)
        liked = False
    else:
        cocktail.likesadd(request.user)
        liked = True

    return HttpResponseRedirect(reverse('cocktails:cocktail-details', args=[str(pk)]))


class CocktailDetails(LoginRequiredMixin, DetailView):
    model = AddCocktails
    template_name = 'cocktails/cocktail-details.html'

    def get_context_data(self, *args, **kwargs):
        cocktail_data = AddCocktails.objects.filter(id=self.kwargs['pk'])
        context = super().get_context_data(**kwargs)

        stuff = get_object_or_404(AddCocktails, id=self.kwargs['pk'])
        total_likes = stuff.total_likes

        liked = False
        if stuff.likes.filter(id=self.request.user.id).exists():
            liked = True

        context['cocktail_data'] = cocktail_data
        context['total_likes'] = total_likes
        context['liked'] = liked
        return context

urls.py

path('cocktail-details/<int:pk>/', CocktailDetails.as_view(), name='cocktail-details'),
path('likes/<int:pk>/', like_view, name='likes'),

template

{% for cocktail in cocktail_data %}
<h4>Cocktail Name</h4>
{{ cocktail.cocktail_name }}
<h4>Cocktail Category</h4>
{{ cocktail.cocktails_category }}
<h4>Type of Glass</h4>
{{ cocktail.crockery_category }}
<h4>Method Category</h4>
{{ cocktail.method_category }}
<h4>Ingredients</h4>
{{ cocktail.ingredients }}
<h4>Execution</h4>
{{ cocktail.execution }}
<img src="{{ cocktail.image.url }}" width="350" height="350">
<form action="{% url 'cocktails:likes' cocktail.pk %}" method=POST>
    {% csrf_token %}
    {% if liked %}
        <button type="submit" name="cocktail.id" value="{{ cocktail.id }}" class="btn unlike">Unlike
        </button>
    {% else %}
        <button type="submit" name="cocktail.id" value="{{ cocktail.id }}" class="btn like">Like
        </button>
    {% endif %}
    - {{ total_likes }} Likes
</form> 
{% endfor %}

So after clicking the button the url shows "http://127.0.0.1:8000/likes/3" and page not found Should I create template for like_view?

Is putting Like system good idea inside for loop? Because when I put it outside got error saying Reverse for 'likes' with arguments '('',)' not found. 1 pattern(s) tried: ['likes/(?P<pk>[0-9]+)$']

Upvotes: 0

Views: 130

Answers (1)

NKSM
NKSM

Reputation: 5854

In your template in form you use name="cocktail.id"

Than in your view your do get_object_or_404(...id=request.POST.get('cocktail_id'). cocktail_id does not exist in your query, that's why you get HttpResponseNotFound.

You have to use your button name cocktail.id:

cocktail = get_object_or_404(AddCocktails, id=request.POST.get('cocktail.id'))

or maybe even simpler use pk parameter:

cocktail = get_object_or_404(AddCocktails, id=pk)

Upvotes: 1

Related Questions