【发布时间】:2014-05-21 04:56:22
【问题描述】:
django1.6 的新手
我想在基于类的通用视图 (Listview) 中设置 cookie
models.py
class Designation(Models.model):
title = models.CharField(max_length=50)
description = models.CharField(max_length=10000, blank=True)
views.py
class DesignationList(ListVew):
def get_queryset(self):
"""
will get 'sort_by' parameter from request,
based on that objects list is return to template
"""
col_nm = self.request.GET.get('sort_by', None)
if col_nm:
if cookie['sort_on'] == col_nm:
objects=Designation.objects.all().order_by(col_nm).reverse()
else:
cookie['sort_on'] = col_nm
objects=Designation.objects.all().order_by(col_nm)
else:
objects = Designation.objects.all().order_by('title')
//set cookie['sort_on']='title'
return objects
模板 在模板中迭代对象
所以最初对象显示在 sort_by 'title' desc 中。 “这个值是我想在 cookie 中设置的”。
在模板中,如果用户点击标题,它将签入cookie cookie['sort_on']='title' 然后所有对象都按顺序排列
如果用户点击描述,则 cookie 值被替换 cookie['sort_on']='description' 和对象按 desc 顺序排列..
soo,如何设置我可以在整个 ListView 类中使用的 cookie..?
提前谢谢..
【问题讨论】:
-
我这个上一个问题应该会给你答案。 stackoverflow.com/questions/18875803/…
标签: python django cookies django-views