1👍
I have tried this code in my template:
This kind of complex decision-making goes in the view functions.
Or it goes into the context which is then presented to the template.
https://stackoverflow.com/search?q=%5Bdjango%5D+context
Do this in your view
def my_view( request ):
followup= user.has_perm('followup.add_followup')
# etc.
return render_to_response( template, {'followup':followup,... )
Then your template is simply
{% if followup %}
<li><a href="{% url followup-new p.id %}">Log</a></li>
{% endif %}
11👍
Since you are using the Django permission system, it’s better you use the followingg template syntax…
{%if perms.followup.add_followup%}your URL here{%endif%}
EDIT: Django automatically creates 3 permissions for each model, ‘add’, ‘change’ and ‘delete’. If there exists no model for adding a link, then you must add the permission from related model, in the model class Meta… Likewise:
somemodels.py
class SomeModel(Model):
...
class Meta:
permissions = (('add_followup','Can see add urls'),(...))
In the Django auth user admin page, you can see your permission. In the template layer, permission is presented with the basic Django style,
<app_label>.<codename>
which, in this case, will be like:
{%if perms.somemodels.add_followup%}your URL here{%endif%}
If there is no model, related to the job you wish to do, the add the permission to a model…
In your template, you can write
{{perms.somemodels}}
to seal available permissions to that user, where somemodel
is the name of the applicaton that you add your permission to one of its models.
- [Django]-Uwsgi worker not distributing evenly
- [Django]-Django DB Design – Maintaining common and historical data
2👍
Django documentation detailing answer #2:
https://docs.djangoproject.com/en/dev/topics/auth/#id9
The currently logged-in user’s permissions are stored in the template variable {{ perms }}. This is an instance of django.contrib.auth.context_processors.PermWrapper, which is a template-friendly proxy of permissions.
- [Django]-Django rest framework and forms: How to do
- [Django]-Populate() isn't reentrant Django Google App Engine
- [Django]-Removing unused lookup_field generated from router on viewset
- [Django]-Is there a simple way to create Chained Dynamic Drop Down List in Django Admin Site?
2👍
This is my very simple solution, in your template add this:
for example:
.......
{% if 'user.can_drink' in user.get_all_permissions %}
{{ user }} can drink.
.......
{% else %}
{{ user }} can´t drink.
........
{% endif %}
.......
- [Django]-Django AMQP error
- [Django]-Django CONN_MAX_AGE setting error
- [Django]-Django: Formset for adding captions to uploaded images
- [Django]-(Djoser) Weird activation email when I update the user fields
- [Django]-Django: In django-tables2 how to modify the way URLField gets rendered