Answer the question
In order to leave comments, you need to log in
How to make bookmarks based on session and cookies in Django?
Good day, I am new to Django and have not come across this before, the bottom line is that I do not have a personal account and authorization in the project, but I need the bookmark functionality.
There is such a code, please help me to make it work for session.
class Addbookmark(View):
def post(self, request, pk,):
child = Child.objects.get(id=pk)
new_bookmark = BookmarksModel.objects.create(child=child)
return redirect('/bookmarks/all-bookmarks/')
class BookmarksListView(ListView):
model = BookmarksModel
context_object_name = 'bookmarks'
template_name = 'bookmarks/bookmarks.html'
Answer the question
In order to leave comments, you need to log in
class Addbookmark(View):
def post(self, request, pk):
child = Child.objects.get(id=pk)
new_bookmark = BookmarksModel.objects.create(child=child)
session_bookmarks = request.session.get("bookmarks_id", [])
session_bookmarks.append(new_bookmark.id)
request.session['bookmarks_id'] = session_bookmarks
return redirect('/bookmarks/all-bookmarks/')
class BookmarksListView(ListView):
model = BookmarksModel
context_object_name = 'bookmarks'
template_name = 'bookmarks/bookmarks.html'
def get_queryset(self):
return self.model.objects.filter(id__in=self.request.session.get("bookmarks_id", []))
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question