我正在使用django / python构建网站。该网站包括一些网页(例如P1和P2),其中p2是p1可以访问的形式,并且链接到p1的链接将通过电子邮件和消息提供给特定用户。我想阻止除特定用户之外的其他人进入网站的p1页面。考虑一下我已将链接授予某位用户x1的情况,他使用该链接并阅读p1页并在p2页上填写表格,现在链接应该过期并且通常,如果在浏览器中为任何用户输入URL https://www.website.com/home/p1则不能直接访问。这样就可以确保这些页面无法被他人访问。这是一个相当复杂的情况,应该如何使用Django身份验证? (请注意,我的网站没有指向p1或p2的单个链接,仅生成的链接可以访问p1,然后通过p1通过p2可以生成这样的链接吗?)
答案 0 :(得分:0)
为什么在模板中不做类似的事情?
基于用户的布尔值,您可以在用户访问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 %}