Django-只允许从特定页面访问URL [英] Django - allow url only to be accessed from a specific page

查看:239
本文介绍了Django-只允许从特定页面访问URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表格供用户填写<主页> /数据。
但是,我希望用户只有在同意某些条款(位于<主页> /条款。

I have a form for the user to fill out at < home >/data. However, I want the user to only be able to access the form if they agree to certain terms, located at < home >/terms.

是否有一种方法可以确保访问<主页> /数据来自<主页> /术语?即只需输入<主页> /指向地址栏的数据应引发错误或重定向至术语。

Is there a way to ensure that the only way to access < home >/data is from the agree button on < home >/terms? I.e. just typing in < home >/data to the address bar should throw an error or redirect to terms.

如果有关系,我将不存储用户(我正在使用会话数据)。

In case it matters, I'm not storing users (I'm using session data).

谢谢!

推荐答案

您可以使用 referer 标头( request.META ['HTTP_REFERER'] )确定用户来自哪个页面。您的视图可以读取该标头,如果该标头不存在,则拒绝访问,并重定向回您的条款页面。

You can use the referer header (request.META['HTTP_REFERER'])to determine which page the user came from. Your view can read this header and deny access if it doesn't exist / redirect back to your terms page.

对于更可靠的信息,如果您已经使用会话数据,您可以在用户单击条款页面上的同意按钮后将标记存储在会话中。也许这样做是POST形式,您在他们同意的会话中设置了一个标记,然后视图重定向到/ data页面。

For something a bit more reliable, if you're already using session data, you can store a flag in the session upon the user clicking the "agree" button on the terms page. Perhaps that does a form POST, you set a flag in the session that they agreed, and the view redirects to the /data page.

在/ data视图中,您如果在会话中未检测到此标志,则可以重定向回条款页面。

On the /data view, you can redirect back to the terms page if this flag isn't detected in the session.

这篇关于Django-只允许从特定页面访问URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆