I have a dictionary that I pass to Django template.
If I render just a dictionary with {{dict}} , then it is rendered fine, like this:
If I render just the keys with {{appoint}}, they are also rendered just fine, like this:
But if I render dictionary values with {{dict.appoint}} then I get nothing
I have read every post here about Django, template language and dictionaries and have not been able to solve this seemingly simple problem.
Render your dict in html like this:
{% for key, value in dict.items() %}
<tr>
<td> {{ key }}: </td> <td> {{ value }} </td>
</tr>
{% endfor %}
you can do with a custom template tag, check in here how to do it https://docs.djangoproject.com/en/3.0/howto/custom-template-tags/
Related
I have a list of dictionaries with same key information. How can I pass this list of dictionaries to a Django template ?
listdict = [{'product':'specs','price':'12'}, {'product':'shoes','price':'30'}]
When trying to send this list via views file to template it fails with an error indicating that only dictionaries are allowed.
Here is the code from views file
return render(request, 'recordings/extensionrecording.html',listDict)
Here is the html block-
<tbody>
{%for list in listDict%}
{%for elements in list%}
<tr>
<td class="body-item fonts-style display-4">{{elements.product}}</td>
<td class="body-item fonts-style display-4">{{elements.price}}</td>
</tr>
Just pass these as an entry of the dictionary:
return render(request, 'recordings/extensionrecording.html',{'listDict': listDict})
Then in the template, you can render these with:
{%for item in listDict %}
{{ item.product }}
{{ item.price }}
{% endfor %}
I will try my best to be as concise as possible.
In my back end, I have a list of dictionaries saved to a variable. Each dictionary represents a post from reddit and includes the score, url, and title.
Respectively, the template will loop through this list and then return the values of each of these keys to the user like so:
<table>
<tr>
{% for x in data[0:5] %}
<td>
{{ x['score'] }}
{{ x['title'] }}
<br>
<a href='/add_to_favorites'> Add To Favorites </a>
</td>
{% endfor %}
</tr>
</table>
As you can see, there's an tag which is linked to a function on my utils.py that is attempting to save the respective dictionary to the database (I have a model that represents the url, title, and score).
I feel as though my template is not representing the dictionary in the correct way, for my link to include the html as when it is pressed I receive a 404 error (though I have this route already defined in views.py - '/add_to_favorites' which calls my 'save_post' function).
def save_post():
data = get_info()
for post in data:
fav= Favorite(title=post.get('title'), url=post.get('url'), score=post.get('score'), user_id=current_user.id)
db.session.add(fav)
db.session.commit()
return redirect(url_for('favorites'))
and:
#app.route('/add_to_favorites')
#login_required
def add_to_favorites():
return save_post()
Am i going about this the wrong way? How can i make sure that the link/button is associated with only the html of the that it is included in?
Just need some guidance into the right direction here, not necessarily the code to fix it. Thank you
Hello I am using jinja2 and I have a list of dictionaries that I am passing to jinja template.
{%for dict in list_of_dicts%}
<tr>
{%for key in my_dict.keys()%}
<td> {{my_dict.key}}</td> // Here i want to get value in my_dict with that particular key.
{%endfor%}
</tr>
{%endfor %}
How do I achieve this?
By using just one variable in your for-loop, you just get the value and not the key name. You can use the alternative key, value syntax (as documented here) in a for loop to get the keys in a separate variable, like this:
{% for key, value in my_dict.iteritems() %}
<td>{{ key }}</td>
{% endfor %}
I am trying to update variables inside of my index.html file. I am going to be running a thread with a loop in python but I want a way to update my jinja2 table listed below to update every x seconds just like if you were using php and ajax.
Here is my Jinja2 Code:
<table border=1 id="allTable" class="display">
<tbody id="eliteTable">
<tr><td colspan=9 class=queueheader>Elite (Current SLA: {{ eliteSLA | safe}}%)</td></tr>
<tr><th>Skill</th><th>SLA</th><th>Calls Waiting</th><th>Hold Time</th><th>Staffed</th><th>Avail</th><th>ACW</th><th>Aux</th><th>ACD Calls</th></tr>
{% for row in eliteList %}
{% if row[2]|int > 30 %}
<tr class=longwait>
{% elif row[2]|int > 0 %}
<tr class=waiting>
{% else %}
<tr>
{% endif %}
{% for i in row %}
<td> {{ i | safe }} </td>
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>
You'll need some javascript in there.
Either ajax requests, or websockets, though ajax might be simpler.
Simply use javascript setInterval() with an ajax request.
I'd recommend using a library, maybe jquery as it is very simple.
$.get( "/auto_refresh", function( data ) {
alert( "Data Loaded: " + data );
});
Note that jinja2 is just for the templating, meaning that at some point the jinja templates get translated into html/css.
So you can play with ajax like you did when you were using PHP.
Jinja variables are generated at template render-time. There's no way to programmatically update them without using javascript of some sort.
From the docs (emphasis mine):
A [Jinja] template contains variables and/or expressions, which get replaced with values when a template is rendered; and tags, which control the logic of the template. The template syntax is heavily inspired by Django and Python.
I am trying to send a zipped list in context to my HTML template. But I'm only seeing the data of the latest object and not all the objects. Document is my model name.
I am trying to scrape a HTML page and the values I've received are being stored in list (I'm using lxml to parse).
Here is the relevant code from views.py
query_results=Document.objects.order_by('-id')
zipped=Document.objects.all().distinct().values_list('zipped')
context ={
"query_results": query_results, "zipped": zipped,
}
return render(request, "result.html", context)
Here is how I'm displaying the data in my template's table.
{% for item in query_results %}
<tbody>
<tr>
<td>...</td>
.
.
<td colspan="2">
{% for i,j in zipped %}
<b>{{ i }}</b> : {{ j }}<br><br>
{% endfor %}
</td>
.
.
</tr>
</tbody>
{% endfor %}
This is how I zipped the two lists.
Mymodels=Document()
Mymodels.zipped = zip(Mymodels.anchortext,Mymodels.anchorlink)
I'm supposed to get different values for my 3 rows but I'm getting the same value for all the 3 rows. The other values are fine only the zipped values are the same.
Any help would be greatly appreciated.