用户点击按钮时创建cookies.

2024-09-29 22:25:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我希望当一个人在我的页面上单击一个按钮时,视图中的一个函数被调用,它在用户的机器上创建cookie,然后将cookieid存储在我的模型中。你知道吗

模板(HTML页):

<form action="#" method="get">
        <p style="text-align: center;"> 
            <input type="submit" class="optOutButton" value="Opt-Out" name="optOutButton"> </p>
    </form>

你知道吗视图.py你知道吗

class UserOptOut(TemplateView):
    template_name = 'debug/opt-out-child.html'    

    def user_opt_out(request):
        if(request.GET.get('optOutButton')):
            response = HttpResponse('Hahahaha')
            response.set_cookie('id', 1)
            return response
        if request.COOKIES.has_key( 'id' ):
            value = request.COOKIES[ 'id' ]
        return render_to_response(debug/opt-out-child.html)

但是,当我点击按钮时什么也没发生。有人能帮我做错事吗?你知道吗


Tags: namedebugform视图idgetvaluecookie
1条回答
网友
1楼 · 发布于 2024-09-29 22:25:29

user_opt_out不是在TemplateView中自动调用的方法。您可能应该将其放在get方法中。你知道吗

尽管既然显式呈现并返回方法的完整响应,我还是不知道为什么使用基于类的视图:只使用普通的视图函数。你知道吗

相关问题 更多 >

    热门问题