Define variables in template based on user being staff or not - python

I'm trying to display an HTML table of values with about 20 columns where say staff users see one subset of columns, and non-staff users see another subset of columns. I may want to define further types of users later. Now right now I have three static header rows so the template looks like
<table>
<tr>
<th>Col A</th>
{% if user.is_staff %}<th>Col B</th>{% endif %}
...
{% if not user.is_staff %}<th>Col K</th>{% endif %}
</tr>
<tr>
<td>Col A second header</td>
{% if user.is_staff %}<td>Col B second header</td>{% endif %}
...
{% if not user.is_staff %}<td>Col K second header</td>{% endif %}</tr>
<tr><td>Col A third header</td> ... </tr>
{% for obj in object_list %}
<tr>
<td>{{ obj.col_a }}</td>
{% if user.is_staff %}<td>{{ obj.col_b }}</td>{% endif %}
...
{% if not user.is_staff %}<td>{{ obj.col_k }}</td>{% endif %}
</tr>
{% endfor %}</table>
However, I find non-DRY as every time, if I want to change if a user-type can see a column, I have to change it in 4 places. Or if I want to define multiple different classes of users, I'd have to have complicated if statements. I'd prefer something like
{% if show_col_a %}<td>{{obj.col_a }}</td>{{% endif %}
Where I can define at the top of the template (or possibly in the view) that user.is_staff can see show_col_a. Is something like this possible? I'm using a generic view (object_list). Maybe modify all users to have attributes user.show_col_a somehow and do {% if user.show_col_a %}? I'm not sure how to add boolean attributes to users.
EDIT: May want multiple users with custom views (e.g., staff_view; admin_view, unprivileged, etc.), so if statements would get unwieldy. A cell's contents is typically more complicated than {{ obj.col_b }}; tried simplifying problem to get to the point. E.g.:
<td>{% if obj.custom_address %}
{{ obj.custom_address.webprint|safe }}
{% else %}
{{ obj.business.address.webprint|safe }}
{% endif %}</td>
Also while multiple templates would work with a simple switch like:
{% if user.is_staff %}
{% include "template_staff.html" %}
{% else %}{% if user.is_admin %}
{% include "template_admin.html" %}
{% else %}
{% include "template_other.html" %}
{% endif %}
{% endif %}
I find its not DRY at all; e.g., every edit to a template has to be replicated in three template. I guess I could make a script that read generates the three templates from some super_template outside of django but its getting very inelegant.

This depends a lot on what view you have and templates.
Ways to do:
make a public template and staff template and add a simple method to change the templates on the fly for the views.
make a template tag:
{% is_staff myvar %}
tag code:
class IsStaffNode(Node):
def __init__(self, var):
self.var = var
def render(self, context):
if context['user'].is_staff():
return var.resolve(context)
return ""
#register.tag
def is_staff(parser, token):
var = parser.compile_filter(token.split_contents()[1])
return IsStaffNode(var)
Homework: make it a block tag to include the td's so that it's shown either all or none.
{% isstaff myvar %}<td>{{ myvar }}</td>{% endisstaff %}
This way is more labor intensive than 2 different templates, but if you want to try, manipulating the context (or creating a separate context for the block only) might be useful.
Make a context processor that would fill the context with some variables if the user is staff, or not if not.
Make a tag that would include the template (inherit from IncludeNode) and manipulate the context.

Related

Django TemplateSyntaxError in with template tag

I'm trying to use this app in my project.
https://github.com/streema/django-favit
I already can use the fav-unfav part of this app. I also want to list favourites of user for every user. In read me part it says use this and it will be listed but I have an error with
{% with user_favorites <user> "baslik.Entry" as favorite_list %}
{% for fav_obj in favorite_list %}
{{ fav_obj }}
{% endfor %}
{% endwith %}
Error:
TemplateSyntaxError at /
u'with' expected at least one variable assignment
This is the template tag part for user_favorites:
#register.assignment_tag
def user_favorites(user, app_model=None):
"""
Usage:
Get all user favorited objects:
{% with user_favorites <user> as favorite_list %}
{% for fav_obj in favorite_list %}
{# do something with fav_obj #}
{% endfor %}
{% endwith %}
or, just favorites from one model:
{% with user_favorites <user> "app_label.model" as favorite_list %}
{% for fav_obj in favorite_list %}
{# do something with fav_obj #}
{%
{% endwith %}
"""
return Favorite.objects.for_user(user, app_model)
How can I get rid of this error? Thanks.
It's a reasonably common convention in documentation that anything in angle brackets is a placeholder to be replaced by the actual value. In this case, <user> is supposed to be replaced by the object containing the actual user.
{% with user_favorites request.user ...
I must say, though, that the documentation still doesn't make any sense. You can't use an assignment tag in a with statement like that - even after correcting the user issue, this still won't work. The confusing thing is that the same syntax is repeated throughout the documentation, but it simply doesn't work.
I think this is simply a bug with the documentation, and suspect that if you simply remove the word "with" this will work.
To use custom template tag in django, it is needed to explicitly load it in template.
Add this line at the beginnig of your template (but after {% extends ... %}, if you have such):
{% load favit_tags %}
Looks like this step is missed from django-favit README.

Django filter field on the context processor

I want to filter a result of a field in django in an html file.
Something like this
{{ model.field where id = 2 }}
I've been looking for in django docs but i only could find a way to do it on the views.py.
I also so something like javascript when u write a "|" simbol after the request but i still couldnt archieve it
You can use the {% if %} template tag. So:
{% if model.field == 2 %}
# do something
{% endif %}
Here is the official documentation:
https://docs.djangoproject.com/en/dev/ref/templates/builtins/#operator
Edit:
If model.field has a value of 2 then it just needs to be the above.
Edit 2:
Without seeing your code, it is hard to tell, but here is how to filter for Users based on Gender in a template:
{% for user in users %}
{% if user.gender == "male" %}
# do something
user.username
{% endif %}
{% endfor %}

create templates on the fly / dynamically

I am currently writing a django app around a rather complex data model.
For many use cases, I need to build similar, but slightly differing templates (incl. graphviz etc..).
Now I wonder if there is a way to follow DRY and create the templates "on the fly", e.g. based on a nested tuple (of which one gets defined per use-case -> template).
Practically speaking I'd like to stop writing these:
static_template_case1.html
{% if program %}
{{ program.name }}
{% for process in program.process_set.all %}
{{ process.name }}
{% for step in process.step_set.all %}
{{ step.name }}
{% endfor %}
{% for control in process.control_set.all %}
{{ control.name }}
{% endfor %}
{% endfor %}
{% endif %}
and replace it with something in the following direction:
template_generator.py
structure_case1 = ("program"("process"("step","control")))
def onTheFlyTemplate(structure):
# iterate through structure
# build template dynamically
# return template
onTheFlyTemplate(structure_case1)
I was not able to find something similar and also don't know yet where to start generating templates on the fly, nor how to integrate it.
Within the views feels like a good starting point, (instead of loader.get_template('static_template_case1.html')).
Thanks for any hint sending me into the right direction as well as your thoughts if DRY is being "overdone" here.
Ralph
Edit
I got a step closer, doing the following:
In the view:
from django.template import Template
def templateGen():
return Template("Program: {{program.count}}")
#login_required
def test(request):
program = Program.objects.all()
t = templateGen()
c = RequestContext(request,locals())
return HttpResponse(t.render(c))
So far this is only a proof of concept. The real work will be to write a smart "templateGen()" listening to the nested tuple.
How about an inclusion tag?
#register.inclusion_tag('<your-template.html>')
def get_program(program_id):
return {'program': <some logic to return your Program object>}
Your main template:
{%load my-template-tags%}
<div id="my-div">
{%get_program program_id %}
</div>
And your template file:
{{ program.name }}
{% for process in program.process_set.all %}
{{ process.name }}
{% for step in process.step_set.all %}
{{ step.name }}
{% endfor %}
{% for control in process.control_set.all %}
{{ control.name }}
{% endfor %}
{% endfor %}
You'd have to pass in your program_id to the main template.

Dynamic session access in templates

I'm trying to access session keys within a loop that needs to be dynamic, I think you'll get what I'm going for by looking at my code that isn't working.
{% for q in questions %}
<div class="question_wrap">
<h2>{{ q }}</h2>
# this does not work
{% if not request.session.get(str(q.id), False) %}
<!-- show them vote options -->
{% else %}
<!-- dont show options -->
{% endif %}
</div>
{% endfor %}
The syntax of Django templates is very limiting in order to prevent people from putting too much logic inside templates and doesn't allow you to pass parameters to methods.
You can prepare a list of tuples already in the view or write a simple template tag for that. The first options is usually easier:
In the view:
questions = [(q, request.session.get(str(q.id), False)) for q in questions]
In the template:
{% for q, has_voted in questions %}
...
{% endfor %}

In Django, where is the best place to put short snippets of HTML-formatted data?

This question is related to (but perhaps not quite the same as):
Does Django have HTML helpers?
My problem is this: In Django, I am constantly reproducing the basic formatting for low-level database objects. Here's an example:
I have two classes, Person and Address. There are multiple Addresses for each Person, setup likeso (in their respective models.py)
class Person(models.Model):
...
class Address(models.Model):
contact = models.ForeignKey(Person)
Now, whenever I look at a Person, I want to see all their Addresses. So suppose Persons/views.py has something likeso:
def detail(request, person_id):
person = get_object_or_404( Person, pk=person_id )
return render_to_response('persons/details.html',
{ 'title' : unicode(person), 'addresses': person.address_set.all() } )
And, I have a template, persons/details.html, with code, for example, like-so:
{% extends "base.html" %}
{% for address in addresses %}
<b>{{ address.name }}</b>
{{ address.type }} <br>
{{ address.street_1 }}<br>
{{ address.street_2 }}<br>
{{ address.city }} {{ address.stateprov }} {{ address.postalcode }}<br>
{{ address.country }}
<hr>
{{ endfor }}
I am repeating this code quite a bit, often with minor variations, such when it's in a table, and then < br > must be substituted by < /td >< td >. Other times, I don't want a street_2 to display (or the < br > after it). All to say, there is fundamental logic that I want to express, that I am even more loath to tote around with block-and-copy!
What I want is a persons/details.html with, for example, the following:
{% extends "base.html" %}
{% for address in addresses %}
{% address.as_html4 %}
{% endfor %}
And if I want inline table, something likeso (I guess!):
{% extends "base.html" %}
<table><tr>
{% for address in addresses %}
<tr><td> {% address.as_html4 </td><td> %} </td></tr>
{% endfor %}
</table>
The question is, then: Where is the best place to put the formatting? The logic?
Django seem to have the following (plausible) options:
Put the formatting in models.py
Put the logic/formatting in views.py
Put the logic/formatting in some other sub-class of Person or Address (i.e. addresses/html4.py)
Create custom tags
Help / insight much appreciated!
Sounds like an inclusion tag is what you're looking for. You could have a template and tag for each major variation and use the tag's arguments to customise the context for each template as required.
Basic tag definition:
#register.inclusion_tag('person/address.html')
def display_address(address):
return {'address': address}
Use in templates (assuming the templatetag module containing it has already been {% load %}-ed):
{% display_address address %}
I would use a template tag outputting data using a template html-file a k a inclusion-tag
I think template filter will be useful too. You can pass filter on each object, for example:
{{ value|linebreaks }} # standard django filter
Will produce:
If value is Joel\nis a slug, the output will be <p>Joel<br>is a slug</p>.
See Django Built-in template tags and filters complete reference.

Categories

Resources