2013-08-12 66 views

回答

3

您可以覆蓋你需要的任何方法,並把該Cookie相關的事情出現了, 例如getdispatch,因爲RedirectView的從django.views.generic.View繼承。

例如,覆蓋調度

class Yourclass(RedirectView): 

    def dispatch(self, request, *args, **kwargs): 
     #youknow how to set cookie here 
     return super(Yourclass, self).dispatch(request, *args, **kwargs) 
2

我解決了我的問題。

def get(self, request, *args, **kwargs): 
    response = super(PartnerRedirectView, self).get(request, *args, **kwargs) 
    max_age = 365 * 24 * 60 * 60 #one year 
    expires = datetime.datetime.strftime(datetime.datetime.utcnow() + datetime.timedelta(seconds=max_age), "%a, %d-%b-%Y %H:%M:%S GMT") 
    response.set_cookie(key, value, max_age=max_age, expires=expires) 
    return response