Django add to wishlist

i have to do wishlist, i have done wishlist page,model and html.But when i click on the button bellow my post, im redirected to wishlist page and post didnt saved in my wishlist.So thats my code:

class Wishlist(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL,
    wished_item = models.ForeignKey(Posts, on_delete=models.CASCADE)

def __str__(self):
    return self.wished_item.title

class Posts(models.Model):
    TYPE = Choices(
        ('private', _('private')),
        ('business', _('business')),
    STATUS = Choices(
        ('active', _('active')),
        ('deactivated', _('deactivated'))
    owner = models.ForeignKey(settings.AUTH_USER_MODEL, blank=True, null=True, related_name='posts',
                              on_delete=models.CASCADE, verbose_name='owner')
    phone_number = PhoneNumberField(verbose_name=_('Phone_number'), null=False, blank=False, unique=True)
    title = models.CharField(verbose_name=_('Title'), max_length=100)
    text = RichTextField(verbose_name=_('Text'))
    image = models.ImageField(upload_to='images/%Y/%m/%d/', null=True, blank=True, validators=[file_size])
    price = models.DecimalField(verbose_name=_('Price'), decimal_places=2, max_digits=9)
    status = models.CharField(choices=STATUS, max_length=50)
    created = models.DateTimeField(auto_now=True)
    type = models.CharField(choices=TYPE, max_length=50)

    def __str__(self):
        return self.title

class WishListView(generic.View):
    def get(self, *args, **kwargs):
        wish_items = Wishlist.objects.filter(user=self.request.user)
        context = {
            'wish_items': wish_items
        return render(self.request, 'wishlist/wishlist.html', context=context)

def addToWishList(request):
    if request.method == 'POST':
        post_var_id = request.POST.get('object-id')
        post_var = Posts.objects.get(id=post_var_id)

            wish_item = Wishlist.objects.get(user=request.user, post=post_var)
            if wish_item:

            Wishlist.objects.create(user=request.user, post=post_var)

            return HttpResponseRedirect(reverse('wishlist'))


{% extends 'posts/base.html' %}
{% load thumbnail %}
{% block content %}
  {% for item in wish_items %}
  {% if item.wished_item.image1 %}
  <img src="{{item.wished_item.image.url}}" alt="">
  {% endif %}
  {% if item.wished_item.image %}
      <img src="{% thumbnail item.wished_item.image 200x200 crop %}" alt="" />
  {% endif %}
{% endfor %}
{% endblock %}

urlpatterns = [
    path("wishlist/", WishListView.as_view(), name='wishlist'),
    path("add-to-wishlist", addToWishList, name='add-to-wishlist'),

and allposts template with add to wishlist button

    {% for object in object_list %}
    <li>Owner: {{ object.owner }}</li>
    <li>Phone: {{ object.phone_number }}</li>
    <li>Title: {{ object.title }}</li>
    <li>Text: {{ object.text }}</li>
    <li>Type: {{ object.type }}</li>
    <li>Price: {{ object.price }}</li>
    <li>Date: {{ object.created }}</li>
      {% if object.image %}
      <img src="{% thumbnail object.image 200x200 crop %}" alt="" />
      {% endif %}
  <form action="{% url 'add-to-wishlist' %}" method="POST">
    <input type="hidden" name="object-id" value="{{}}">
    <input type="submit" value="Add to Wishlist">

Probably problem with posts ID,but im not sure in that

>Solution :

You can use get_or_create instead of the if else statement for if exist or not. And use get_object_or_404 to make code more clear

from django.shortcuts import get_object_or_404

def addToWishList(request):
    if request.method == 'POST':
        post_obj = get_object_or_404(Post, pk=request.POST.get('object-id'))
        Wishlist.objects.get_or_create(user=request.user, post=post_obj)
        return HttpResponseRedirect(reverse('wishlist'))

Leave a Reply