在django-cms插件中设置cookie

时间:2013-11-13 08:31:42

标签: django cookies plugins django-cms

使用Django 1.5.5,Django-CMS 2.4.2 我为django-cms *(cms.plugin_base.CMSPluginBase)编写了一个插件*。插件创建一些形式,工作正常。但我遇到问题 - 提交表单后 - 如何设置 Cookie

from cms.plugin_base import CMSPluginBase
from cms.plugin_pool import plugin_pool
from poll.models import PagePoll
from poll.forms import PollForm
from django.http import HttpResponse

class PollPlugin(CMSPluginBase):
    """ upload a poll  """
    model = PagePoll
    name = u'Poll'
    render_template = u"plugins/page_poll.html"

def render(self, context, instance, placeholder):
    #upload poll form
    request = context['request']

    form = PollForm(instance.poll, request.POST)

    #validate the form
    if request.method == 'POST':
        if form.is_valid():
            form.save()
            #=SAVE COOKIES HERE=
    else:
        form = PollForm(instance.poll)

    context['form'] = form
    return context

plugin_pool.register_plugin(PollPlugin)

1 个答案:

答案 0 :(得分:0)

答案在这里找到: https://djangosnippets.org/snippets/2541/

问题是 cms-plugin 无法返回响应对象,其中 cookies 需要下注设置 ...所以诀窍是用 HttpResponceRedirect

抛出自定义异常