简体   繁体   English

是否可以在 Django 中添加用户过滤按钮

[英]Is it possible to add a user-filtered button in Django

I have a Base.html template that extends to the rest of my HTML pages in my app, In this base.html file, it has a nav-bar that has a bunch of < a > tags that link to the different pages.我有一个 Base.html 模板,它扩展到我的应用程序中的其余 HTML 页面,在这个 base.html 文件中,它有一个导航栏,上面有一堆链接到不同页面的 < a > 标签。

Is it possible to limit the visibility of this < a > tag based on a user's name or users role on the database or would I need to add this in the function of rendering the page to which the < a > tag loads?是否可以根据用户名或用户在数据库上的角色来限制此 < a > 标记的可见性,或者我是否需要将其添加到呈现 < a > 标记加载到的页面的功能中?

You can do:你可以做:

{% if user.username == 'KyleStranger' %}
    your button here
{% endif %}

or或者

{% if user.is_staff %} or {% if user.is_staff %}

{% if perms.app_label.permission_name %} -> example: perms.myapp.can_edit_post {% if perms.app_label.permission_name %} -> 示例: perms.myapp.can_edit_post

You can use User model in your html file and then use a if condition to check that:您可以在 html 文件中使用User模型,然后使用 if 条件来检查:

{% if request.user.is_authenticated and request.user.id == user.id %}
        Yep!
{% endif %}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM