I have a problem converting from a function-based view to a class-based view, function
VIEWS.PY
@ login_required
def favourite_add(request, id):
post = get_object_or_404(Perfumes, id=id)
if post.favourites.filter(id=request.user.id).exists():
post.favourites.remove(request.user)
else:
post.favourites.add(request.user)
return HttpResponseRedirect(request.META['HTTP_REFERER'])
URLS.PY
urlpatterns = [
path('fav/<int:id>/', views.favourite_add, name='favourite_add'),
]
TEMPLATE.HTML
<div>
<a href="{% url 'favourite_add' perfume.id %}" >Add</a>
</div>
In general, the goal is to get the id of a certain perfume on the page, and using the get_object_or_404 function, I'm pulling its object from the Perfumes database - the post variable. Next, I want to retrieve the id of the logged-in user and check if the id of the above user is in the favourites section of the post variable. If not then add, otherwise remove the user id to the favourites section of the post variable.
CodePudding user response:
A function based one probably works fine for what you require.
Anyway, here is a view that should perform the same job as yours:
urls.py:
urlpatterns = [
path('fav/<int:id>/', views.FavouriteView.as_view(), name='favourite_add'),
]
views.py:
from django.views import View
from django.contrib.auth.mixins import LoginRequiredMixin
class FavouriteView(LoginRequiredMixin, View):
def get(self, *args, **kwargs):
post = get_object_or_404(Perfumes, id=self.kwargs.get('id'))
if post.favourites.filter(id=self.request.user.id).exists():
post.favourites.remove(self.request.user)
else:
post.favourites.add(self.request.user)
return HttpResponseRedirect(self.request.META['HTTP_REFERER'])
I recommend looking at something like https://ccbv.co.uk/ to help you understand class based views