Return an extra argument with the datachart - django

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>

Related

overriding Django change_list_results

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

Django HTML template table rendering

I want to render a table look like this:
<table>
<tr>
<td>some data</td>
<th>special one</th>
<td>some data</td>
...
<td>some data</td>
</tr>
...
</table>
There is a solution that can render all in the same tag.
<table>
{% for rowval in results %}
<tr>
{% for val in rowval %}
<td>{{val}}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
But in my case, there would be a th at the second place for every row of the data, if there is a record.
There is another solution that not as good as the answer below as it keeps partial table, td and tr in the view.
Is there a way to implement this feature?
There are some variables available inside a django template for loop, one of them is named forloop.counter which gives you the current iteration of the loop. You can use this variable to render something differently on the second loop
<table>
{% for rowval in results %}
<tr>
{% for val in rowval %}
{% if forloop.counter == 2 %}
<th>{{ val }}</th>
{% else %}
<td>{{ val }}</td>
{% endif %}
{% endfor %}
</tr>
{% endfor %}
</table>

I was going to render the saved data into html table but found this error

<tbody>
{% for publication in publication_list %}
<tr>
<td>{{%publication.addpublication%}}</td>
</tr>
{% endfor %}
</tbody>
views.py
def publicationlist(request):
context = {'publication_list' : AddPublication.objects.all()}
return render(request,"publication_list.html", context)
TemplateSyntaxError at /addpublication/publicationlist/
Could not parse the remainder: '%publication.addpublication%' from '%publication.addpublication%'
# You have to remove % in your td tag of html
<tbody>
{% for publication in publication_list %}
<tr>
<td>{{publication.addpublication}}</td>
</tr>
{% endfor %}
</tbody>
For tags like for, if...:
{% for %}{% endfor %}
For context variables:
{{ my_context_variable }}

django template view nested list

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 %}

Why doesnt the If statement in django work?

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.