【问题标题】:How to hide success page in Django如何在 Django 中隐藏成功页面
【发布时间】:2012-11-07 06:13:02
【问题描述】:

我想知道是否有人知道:

例如,我有一个用户填写的表单,当他提交时,页面将重定向到“谢谢”页面。一切正常。在 urls.py 我有这一行指出该页面存在:

url(r'^thankyou/$', 'render_form'),

但是,当我输入 url mysite.com/thankyou/ 时,会出现“谢谢”页面......但我需要它仅在我提交表单时出现并在用户尝试直接打开时隐藏它.

请帮忙。提前致谢!

【问题讨论】:

    标签: python html django redirect


    【解决方案1】:

    您可以在重定向之前在表单处理视图中的会话中放置一些内容,并在感谢 URL 中检查它:如果不存在,则返回 403 错误。比如:

    def form_handling_view(request):
        if request.POST:
        form = MyForm(request.POST)
            if form.is_valid():
                ... handle the form ...
                request.session['form_posted'] = True
                return redirect('thank_you')
    
    def thank_you(request):
        if not request.session.pop('form_posted', False):
            return HttpResponseForbidden('not permitted')
        ... render thank_you page ...
    

    请注意,我在thank_you 中使用pop 以确保无论如何都会从会话中删除密钥。

    【讨论】:

    • 是的,它有效,但我必须在thank_you 方法中编辑一些条件。非常感谢丹尼尔。
    猜你喜欢
    • 2022-06-11
    • 1970-01-01
    • 2016-02-17
    • 1970-01-01
    • 2012-08-28
    • 2020-05-06
    • 2017-01-01
    • 1970-01-01
    • 2012-07-24
    相关资源
    最近更新 更多