Home > other >  Is it possible to add a user-filtered button in Django
Is it possible to add a user-filtered button in Django

Time:10-25

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.

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?

CodePudding user response:

You can do:

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

or

{% if user.is_staff %} or

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

CodePudding user response:

You can use User model in your html file and then use a if condition to check that:

{% if request.user.id == user.id %}
        Yep!
{% endif %}
  • Related