I have an application where I am using django-guardian for object-level permission. In my ListView, I am listing all my instances in a table. In each row, I need to show the edit button if the user has the permission to edit this object.
So, I doing something like:
{% extends "base.html" %}
{% load guardian_tags %}
{% block content %}
<table>
<thead>...</thead>
<tbody>
{% for i in items %}
<tr>
<td>{{ i.name }}</td>
<td>
{% get_obj_perms request.user for i as "i_perms" %} <!-- this one -->
{% if "change_item" in i_perms %}
Edit
{% endif %}
</td>
</tr>
{% endif %}
</tbody>
</table>
{% endblock %}
Problem
Doing it that way is not optimized solution from database-wise. This solution makes a database query for each object to get the user permissions. How can I do so but with a single hit to the database to get all the objects' user permissions?
I think you would need to use get_objects_for_user in your view and pass it in to your template via context, eg, in a function based view or as part of your get_extra_context() in a CBV
from guardian.shortcuts import get_objects_for_user
items = Item.objects.all()
permitted_items = get_objects_for_user(request.user, 'change_item', klass=items)
context['permitted_items'] = permitted_items
...
then in your template
{% for i in items %}
<tr>
<td>{{ i.name }}</td>
<td>
{% if i in permitted_items %}
Edit
{% endif %}
</td>
</tr>
{% endfor %}
Related
I am customizing my admin panel a little and want to add an additional column to it, so as per the docs I have to override the change_form_results.html file but how do I do it? all I see is this code in it
<tbody>
{% for result in results %}
{% if result.form and result.form.non_field_errors %}
<tr><td colspan="{{ result|length }}">{{ result.form.non_field_errors }}</td> </tr>
{% endif %}
<tr>{% for item in result %}
{{ item }}
{% endfor %}</tr>
{% endfor %}
</tbody>
what change am i suppose to make to above code
In my models.py Task can have multiple Employer so I use ManyToManyField
In my views.py I got a date generator - it return range of 2 weeks from current day.
So here is my template
<table>
<thead>
<tr>
<th>{% trans 'employer_id' %}</th>
{% for i in date_range %}
<th>{{ i }}</th>
{% endfor %}
</tr>
</thead>
<tbody>
{% for employer in employers %}
<tr>
<td>{{ employer.employer_id }}</td>
{% for t in employer.task_set.all %}
<td>{{ t }}</td>
{% empty %}
<td>0</td>
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>
What I need is when I create a Task and assign it to employers, it should show me in the html table a start_date of Task of each employers for specific days.
So far I got this with codes above:
^date of tasks of these employers are not matching with days in the table header.
Instead of
{% for t in employer.task_set.all %}
<td>{{ t }}</td>
{% empty %}
<td>0</td>
{% endfor %}
you need to iterate over the dates again, since you're not creating <td>'s for each date.
{% for i in date_range %}
{% for t in employer.task_set.all %}
{% if t.start_date == i %}
<td>{{ t }}</td>
{% else %}
<td>-</td>
{% endif %}
{% empty %}
<td>0</td>
{% endfor %}
{% endfor %}
I'm trying to print a chart and to return an argument with the datachart to the template.
Views.py:
def errors(request):
totalErrors = total_errors()
table = table_errors()
return render_to_response('macaco_errores.djhtml', {'totalErrors': totalErrors, 'table': table})
There's no problem to print the chart without the 'table' argument, but with both I get 'False' in template when I load in "load_chart".
EDIT:
Template:
{% block head %}
{% load nvd3_tags %}
{% include_chart_jscss %}
{% load_chart charttype totalesData totalesContainer extra %}
{% endblock %}
{% block body %}
<div align="center">
<h1>Errors</h1>
{% include_container totalesContainer 600 1000 %}
</div>
<div align="center">
<table border="1">
<tr>
<th>Log</th>
<th>Type</th>
</tr>
{% for type,log,date in table.items %}
<tr>
<td>{{ type }}</td>
<td>{{ log }}</td>
</tr>
{% endfor %}
</table>
</div>
{% endblock %}
{% load_chart charttype totalesData totalesContainer extra %}
This tag is needing four variables in the context: charttype, totalesData, totalesContainer, extra
It appears you are not passing any of them from your view function (which provides only totalErrors and table)
So there is no way that this can work from the code you've posted.
See this example from the django-nvd3 docs for the type of context data you need to provide from your view function:
http://django-nvd3.readthedocs.org/en/latest/introduction.html#example-how-to-create-a-piechart
Please see also the Django docs for how to pass variables from the view function into the template for rendering.
For example in your view you are currently using the render_to_response helper which takes a dict of variable names and values to pass to the template... these are the only vars which are available in the template.
The solution was:
def counterroresmacaco(request):
errores = errores_macaco()
errores['tabla'] = tipoerror_ticker()
return render_to_response('macaco_errores.djhtml', errores)
'errores' is a dictionary, then in the template I use a for to iterate over 'tabla'.
The problem to pass two arguments (as I was doing) is that django-nvd3 doesn't recognize the arguments like in the examples.
{% load nvd3_tags %}
{% include_chart_jscss %}
{% load_chart charttype totalesData totalesContainer extra %}
{% include_container totalesContainer 600 1000 %}
<table border="1">
<tr>
<th>Tipo de log</th>
<th>Ticker</th>
</tr>
{% for reg in tabla %}
<tr>
<td>{{ reg.log }}</td>
<td>{{ reg.ticker }}</td>
</tr>
{% endfor %}
</table>
i made a nested list and from that id like to make a table in my template
list looks something like
ground_skills_available = [[category1, [skill1, skill2, skill3]], [category1, [skill1, skill2]]]
Now i want to list it that you got category with below the items, next category and other items. Problem is, i have no clue about hot to show only category, instead of category +all items, since you cant seem to use the index of a list?
Can someone please help me out?
<table>
{% for categories in ground_skills_available %}
{% for category in categories %}
<tr>
<td>{{ category }}</td>
</tr>
{% for skill in category %}
<tr>
<td>{{ skill.name }}</td>
</tr>
{% endfor %}
{% endfor %}
{% endfor %}
Change your outer loop to
{% for category, skills in ground_skills_available %}
This technique is described in the Django for loop docs.
On the first iteration of the loop, this will take the list [category1, [skill1, skill2, skill3], and assign
category = category1
skills = [skill1, skill2, skill3]
You can then display {{ category }}, and loop through skills.
Putting that together, you have:
<table>
{% for category, skills in ground_skills_available %}
<tr>
<td>{{ category }}</td>
</tr>
{% for skill in skills %}
<tr>
<td>{{ skill.name }}</td>
</tr>
{% endfor %}
{% endfor %}
</table>
{% if forloop.index == 1 %} ... some code ... {% endif %}
or
{% if forloop.index0 == 0 %} ... some code ... {% endif %}
Expectation:
{% for categories in ground_skills_available %}
{% for category in categories %}
{% if forloop.first %}
<tr>
<td>{{ category }}</td>
</tr>
{% else %}
{% for skill in category %}
<tr>
<td>{{ skill.name }}</td>
</tr>
{% endfor %}
{% endif %}
{% endfor %}
{% endfor %}
I was working from an example of an 'if' statement, but for some reason I cannot get it to work.
I tried every possible statement in the 'if', but it seems as though nothing makes it true for the if statement to run. Instead the Else statement runs.
Sample
{% extends 'base.html' %}
{% block content %}
<h2>Hardware Inventory</h2>
{% if hardware %}
<table id="datatable">
<thead>
<tr>
<th>Name</th>
<th>Manufacturer</th>
<th>Category</th>
</tr>
</thead>
<tbody>
{% for item in hardware %}
<tr>
<td>{{ item.name }}</td>
<td>{{ item.manufacturer }}</td>
<td>{{ item.kind }}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% else %}
<p>The inventory is empty.</p>
{% endif %}
{% endblock %}
Mine
{% extends 'base.html' %}
{% block content %}
<h2>News</h2>
{% if entry %}
{% for item in entry %}
<table id = "news">
<tr>
<td>{{ item.title }}</td>
<td>{{ item.body }}</td>
<td>{{ item.pub_date }}</td>
</tr>
</table>
{% endfor %}
{% else %}
<p>No News</p>
{% endif %}
{% endblock %}
My view.py for news, Im not sure how to write it correctly, but i tried different combinations, this one errors at the moment causes it to crash
def index(request):
return render_to_response('news/index.html', {'Entry': Entry}, context_instance=RequestContext(request))
def Entry(request):
Entry = Entry.objects.all().order_by('pub_date')
return render_to_response('news/Entry.html', {'item':item}, context_instance=RequestContext(request))
Make sure you're actually passing entry into the context. For example:
render_to_response('template.html', { 'entry': entry })
Unset variables behave as variables set to None in Django templates.
UPDATE:
Made some revisions to your view code; not even sure how you got to the template rendering with what you had.
Original:
def index(request):
return render_to_response('news/index.html', {'Entry': Entry}, context_instance=RequestContext(request))
def Entry(request):
Entry = Entry.objects.all().order_by('pub_date')
return render_to_response('news/Entry.html', {'item':item}, context_instance=RequestContext(request))
Modified:
def index(request):
entry = Entry.objects.all().order_by('pub_date')
return render_to_response('news/index.html', {'entry': entry}, context_instance=RequestContext(request))
I don't think you even needed the Entry method, so removed that. I kept your naming the same, but it's better form to call that variable entries since it's multiple items.