0

I would like to know if it possible to send a model or a list using django.

I know that doing this is certainly not the best way. But I need to now.

I'm trying to do this

def article(request):
    articles = Article.objects.all()
    response = render(request, 'someHtml.html',{'articles': articles})
    if request.POST:
        response.set_cookie('articles', articles)
    return response

but doing this way the model article turns string and I would like a django model, like a bellow.

def another_fuction_view(request):
    if 'articles' in request.COOKIES['articles']:
        articles = request.COOKIES['articles']
    else:
        articles = ['not exists']
    return render(request, 'anotherview.html', {'articles': articles})

I hope my arm does not fall because of it heuheuhe

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
  • 1
    You can not simply put a queryset (materialized or not) into a cookie, you need a way to *serialize* it. But this is simply not a good idea at all. Perhaps you can store the `id`s of the articles in a cookie, but I would definitely not pickle them into a cookie. Furthermore note that a user can remove the cookies, or the browser might reject these. – Willem Van Onsem Jun 19 '18 at 20:06
  • you can use it by serializing the model object. https://stackoverflow.com/questions/757022/how-do-you-serialize-a-model-instance-in-django – KAYKISIZ Jun 19 '18 at 20:19

0 Answers0