【问题标题】:How to provide access of special information page to specific users如何向特定用户提供特殊信息页面的访问权限
【发布时间】:2019-09-13 10:02:36
【问题描述】:

我正在使用 django/python 构建网站。该网站包括一些网页(例如 P1 和 p2),其中 p2 是可以由 p1 访问的表单,并且指向 p1 的链接将通过电子邮件和消息提供给特定用户。我想阻止特定用户以外的其他用户访问网站的 p1 页面。考虑我已将链接提供给某个用户 x1 的情况,他使用该链接并阅读 p1 页面并在 p2 页面上填写表格,现在链接应该过期并且通常如果在浏览器中为任何用户输入 URL https://www.website.com/home/p1,则不应直接访问。这将完美地使这些页面免受其他人的访问。这是相当复杂的情况,应该如何使用 django 身份验证呢? (请注意,我的网站没有指向 p1 或 p2 的单个链接,只是生成的链接将是访问 p1 的方式,然后 p2 通过 p1 我们可以生成这样的链接吗?)

【问题讨论】:

  • 你能发布你已经尝试过的代码吗?另外,访问您网站的用户是否已登录?

标签: python django authentication


【解决方案1】:

为什么不在你的模板中做这样的事情呢?

根据用户的类布尔值,您可以在他们访问 url 时向他们显示或隐藏某些内容。

{% if user.is_authenticated and profile != False and user.profile.class1 == True%}

<p>Content which you want to show one class of user. </p>

{% elif user.is_authenticated and profile.class1 == False %}

<p>content which you want to show another class of user</p>

{% else %}

<p><a href="{% url 'login' %}?next={{request.path}}">Log in</a> with your account to view this information.  </p>

{% endif %}

【讨论】:

    猜你喜欢
    • 2010-11-01
    • 2013-03-09
    • 1970-01-01
    • 2011-08-08
    • 2020-01-07
    • 1970-01-01
    • 1970-01-01
    • 2020-08-26
    • 2019-11-24
    相关资源
    最近更新 更多