Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set cookie in class based generic view

newbies to django1.6

i want to set cookie in class based generic view (Listview)

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

template in template im iterating over objects

so initially objects display in sort_by 'title' desc. "this values is i want to set in cookie".

in template, if user click over title,it will check in cookie cookie['sort_on']='title' then all objects are in asce order

if user click over description,then cookie value is replaced cookie['sort_on']='description' and objects are in desc order..

soo,how to set cookie which i can use in whole ListView class.. ?

Thnx in advance..

like image 809
Ravi Avatar asked Apr 08 '14 13:04

Ravi


People also ask

What is generic class based view in Django?

In this tutorial, we will introduce the Class-Based Generic views. These are the advanced set of Built-in views and are used to implement the selective CRUD (create, retrieve, update, and delete) operations. Using Class-Based views, we can easily handle the GET, POST requests for a view.

What is a generic view?

The principal of generic views in the study of cognition stipulates that the interpretation made by an observer of a distal phenomenon should be such as to not require that the observer be in a special position to, or relationship with, the phenomenon in question.

What is a class based view?

A view is a callable which takes a request and returns a response. This can be more than just a function, and Django provides an example of some classes which can be used as views. These allow you to structure your views and reuse code by harnessing inheritance and mixins.

Why we use generic views in Django?

Django's generic views were developed to ease that pain. They take certain common idioms and patterns found in view development and abstract them so that you can quickly write common views of data without having to write too much code.


1 Answers

In order to set/delete cookies you have to have access to the "response" object. To do so, in a class-based view, you can override "render_to_response".

Example:

class DesignationList(ListVew):
    def render_to_response(self, context, **response_kwargs):
        response = super(LoginView, self).render_to_response(context, **response_kwargs)
        response.set_cookie('sort_on', 'title')
        return response
like image 191
andreftavares Avatar answered Sep 23 '22 05:09

andreftavares