I can like my products from admin panel,and i want to add posibility to like them by url products/item/like,i have view of like but i dont know what i should add to there.This is my views.py
class LikeToggleView(AjaxResponseMixin, JSONResponseMixin, FormView):
http_method_names = ('post',)
form_class = LikeForm
product = None
@csrf_exempt
def dispatch(self, request, *args, **kwargs):
product_id = kwargs['product_pk']
try:
self.product = Product.objects.get(id=product_id)
except Product.DoesNotExist:
raise Http404()
return super().dispatch(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
pass
this is forms.py
class LikeForm(forms.ModelForm):
user = forms.ModelChoiceField(User.objects.all(), required=False)
product = forms.ModelChoiceField(Product.objects.all())
ip = forms.GenericIPAddressField(required=True)
this is models.py
class Like(TimeStampedModel):
product = models.ForeignKey(Product, related_name='likes')
user = models.ForeignKey(settings.AUTH_USER_MODEL, blank=True, null=True, related_name='likes')
ip = models.GenericIPAddressField(blank=True, null=True)
class Meta:
unique_together = (('product', 'user'), ('product', 'ip'))
def __str__(self):
return '{} from {}'.format(self.product, self.user or self.ip)