I'm using a list to populate / generate some html code dynamically as shown below,
<ul>
{% if results %}
{% for result in results %}
<li><a href="/accounts/evalpage/" ><strong>{{result.0}}</strong>{{result.1}}</a></li>
{% endfor %}
{% else %}
<li><strong>No Evaluations to Perform</strong></li>
{% endif %}
</ul>
I'm running into an issue with that if one of the list items is clicked, i need to be able to retrieve the information stored in that list item, e.g if item one is clicked i need to retrieve {{result.0}} and {{result.1}} is there a way i can retrieve this information?
List is shown below :
[['User1', 'InProgress'], ['User2'], ['User3'], ['User3'], ['User4']]
For instance, if the row containing User1 and InProgress is clicked by the end user, i want to be able to have the information User1 and InProgress within Django such that operations can be performed with them
It should be clear that in order for the backend to know what object you clicked on, you need to pass that value in the URL. So, you'll need to change the definition of your "evalpage" URL to accept a parameter:
url(r'^accounts/evalpage/(?P<user>\w+)/$', views.evalpage, name='evalpage')
and the view signature:
def evalpage(request, user):
...
and now you can do:
...
try this:
<ul>
{% if results %}
{% for result in results %}
<li>
<a href="/accounts/evalpage/" >
{% for item in result %}
<strong>{{item}}</strong>
{% endfor %}
</a>
</li>
{% endfor %}
{% else %}
<li><strong>No Evaluations to Perform</strong></li>
{% endif %}
</ul>
Related
I created a class that has some attributes. One of them is a list that has coordinates as elements.
Here's the python code:
#attr.s
class ProductMetadata:
features: list = attr.ib()
#some other attrs
standard = ProductMetadata(
features=[(True ,"Up to 1000 DMs"), (False, "Ludicrous mode"), (False, "Premium Support")],
# the other attrs
)
And, since I'd like to write a list where the customer can see whereas he can benefit from a feature given his package, here's the code somehow I'd like to write:
<ul>
{% for feature in product.metadata.features %}
<li> <i class="lni {% if the first coordinate of the element is True %}lni-checkmark active"{% else %} lni-close {% endif %}></i>{{ the second coordinate }}</li>
{% endfor %}
</ul>
Here's the result I'd like to get (visually):
Also, given that the class name is ProductMetadata, and that the product.attr works when I just use Stripe (without the class), is using product.metadata.attr correct?
Thanks a lot!
<ul>
{% for feature in product.metadata.features %}
<li> <i class="lni {% if feature.1%}lni-checkmark active"{% else %} lni-close {% endif %}></i>{{ feature.2 }}</li>
{% endfor %}
</ul>
You can use it like this
Consider I have the below multi-dimensional list which I would like to display in Flask HTML Page.
Sample List
list_html = [['Mun--CEO--Bank', 'Chee--CEO--Trust'], ['Miloš--Researcher--College'], ['Mun--CEO--Bank']]
I am trying to display this list in Flask Webpage. Where each item in list is displayed in the next line (please refer the expected output)
Sample Code
{% for dat in list_html %}
<div><span></span><ul><li> {{data[dat]}} </li></ul></div>
{% endfor %}
Here, the data refers to the list_html which I pass it in render_template.
I am unable to display each item as a new line in the output HTML page.
Expected Output
* Mun--CEO--Bank
* Chee-CEO-Trust
* Miloš--Researcher--College
* Mun--CEO--Bank
You just need a nested for loop. Something like this:
{% for dat in list_html %}
<div>
<span></span>
<ul>
{% for d in dat %}
<li> {{ d }} </li>
{% endfor %}
</ul>
</div>
<br>
{% endfor %}
Hope this helps. Good luck.
I am making kind of dynamic menu. when you click menu on the top, it show sub menu on the left side. I searched with keyword 'dynamic menu' from stackoverflow and google. I got idea to build that kind of menu. I made it like below.
1) render data(menu list) in context to template by custom context processor.
2) using custom template tag which is provided by django-mptt package.
3) show top menu in base template.
4) move to another template to show sub menu according to what top menu you click
I made custom context_processor to use menu in context in every template.
context_processor.py
from manager.models import Menu
def menu(request):
menu_list = list(Menu.objects.all())
return {'menu':menu_list}
template.py(example)
{% load mptt_tags %}
<nav id="{{ menu_id }}" class="tree-menu">
<ul>
{% recursetree menu %}
<li class="menu
{% if node.is_root_node %}root{% endif %}
{% if node.is_child_node %}child{% endif %}
{% if node.is_leaf_node %}leaf{% endif %}
{% if current_menu in node.get_descendants %}open{% else %}closed{% endif %}
">
{{ node.menu_name }}
{% if not node.is_leaf_node %}
<ul class="children">
{{ children }}
</ul>
{% endif %}
{% if node.items and node.items.exists %}
<ul class="items">
{% for item in node.items.all %}
{% if item_template %}
{% include item_template %}
{% else %}
{% include "menu/tree-item.html" %}
{% endif %}
{% endfor %}
</ul>
{% endif %}
</li>
{% endrecursetree %}
</ul>
</nav>
mptt_tags.py
#register.tag
def recursetree(parser, token):
"""
Iterates over the nodes in the tree, and renders the contained block for each node.
This tag will recursively render children into the template variable {{ children }}.
Only one database query is required (children are cached for the whole tree)
Usage:
<ul>
{% recursetree nodes %}
<li>
{{ node.name }}
{% if not node.is_leaf_node %}
<ul>
{{ children }}
</ul>
{% endif %}
</li>
{% endrecursetree %}
</ul>
"""
bits = token.contents.split()
if len(bits) != 2:
raise template.TemplateSyntaxError(_('%s tag requires a queryset') % bits[0])
queryset_var = template.Variable(bits[1])
template_nodes = parser.parse(('endrecursetree',))
parser.delete_first_token()
return RecurseTreeNode(template_nodes, queryset_var)
My Question
If you see django manual about QuerySet, it says that "Each QuerySet contains a cache to minimize database access". It is obvious that, if you query same data in certain rule, it doesn't seem hit database again but return result from cache. Then I am querying Menu.objects.all() in custom context processor. This result(menu_list = Menu.objects.all()) will be in context every time, you can use menu data on every template repeately. So does it reuse the result from cache without hitting database again?
If menu_list = Menu.objects.all() in custom context processor hit database every time whenever template load this menu list, Does it work in this way to reuse menu data from cache without hitting database everytime?
context_processors.py
from manager.models import Menu
from django.core.cache import cache
def menu(request):
menu_list = cache.get_or_set('menu_list', list(Menu.objects.all()))
return {'menu':menu_list, 'redis':"Food"}
Lastly, I don't know if there are many people using django-mptt package. I guess just a few people have experience using it in person. It says "Only one database query is required (children are cached for the whole tree)" so does it mean if I use django-mptt package and get menu from it on template, it automatically cache its data?
Well, I am not clear about django cache system.
It would be really appreciate if you can give me answer and insight for my questions. Thanks for reading!
I am trying to use the last.fm API, and the artist search returns the following dictionary with an image:
{u'#text': u'http://userserve-ak.last.fm/serve/34/79694767.png', u'size': u'small'}
However, in my django template, I can't seem to access the value in "#text".
I'm guessing it has to do with the '#' in the name:
My code:
{% for artist in results %}
<li>
{{artist.name}}
{{artist.image.0.#text}}
</li>
{% endfor %}
try this, # shouldnot be the problem:
{% for key, value in results.items %}
{{key}} - {{value}}
{% endfor %}
I'm having problems displaying nested blocks in a template.
eg.
{% for category in categories %}
//code to display category info
{% products = products.object.filter(category = category) %}
{% for product in products%}
//code to display product info
{% endfor %}
{% endfor %}
I'm getting a "Invalid block tag: 'endfor'" error.
Any ideas?
You cannot assign to variables in the Django template system. Your two attempts:
{% products = products.object.filter(category = category) %}
and
{% products = category.get_products %}
are both invalid Django syntax.
Some Python templating systems are PHP-like: they let you embed Python code into HTML files. Django doesn't work this way. Django defines its own simplified syntax, and that syntax does not include assignment.
You can do this:
{% for category in categories %}
//code to display category info
{% for product in category.get_products %}
//code to display product info
{% endfor %}
{% endfor %}
I think you cannot use arguemnts for methods. You have to modify your categories object, so that you kann use:
{% for product in category.products %}
{% products = products.object.filter(category = category) %}
is not recognized as a valid tag in the django template system. Therefore django complains about the missing endfor, although the {% for x in y %) is not the error.
This should work
{% for category in categories %}
{% for product in products.object.all %}
//code to display product info
{% endfor %}
{% endfor %}
But this is not that, what you want to achieve. Simply you are not able to filter on product.objects with the argument category.
You have to write your own tag which takes arguments on filtering or rethink your problem.