I have code for regroup in django template, like below:
{% regroup clients by title.0 as clients_list %}
{% for client in clients_list %}
{{ client.grouper }}
{% for item in client.list %}
{% if item.client_link %}
{{ item.title }}
{% else %}
{{ item.title }}
{% endif %}
{% endfor %}
{% endfor %}
Sometime I have duplicate nametuple in my results for the same letter, for example - 'd':
GroupedResult(grouper='d', list=[<Client: decompany_1>]) GroupedResult(grouper='d', list=[<Client: decompany_2>])
so in result I have two headers with letter 'd'
Someone know, how to avoid this?
edit
views.py
class ClientsListView(ListView):
model = Client
template_name = 'clients/clients_list.html'
context_object_name = 'clients'
Related
I have a custom template tag that accesses a models function. However, I also need the custom template tag to be in a for loop, which requires nested template tags:
{% load custom_tags %}
{% for list in remind_lists %}
<h3>{{ list.title }}</h3>
{% for item in {% get_list_items user.username %} %}
<p>{{ item.title }}</p>
{% endfor %}
{% endfor %}
It gives me a TemplateSyntaxError- 'for' statements should use the format 'for x in y': for item in {% get_list_items user.username. Is there anyway I can do this?
custom tag:
register = template.Library()
#register.simple_tag
def get_list_items(event_ins, authenticated_user):
return event_ins.get_list_items(authenticated_user)
You can't nest tags in this way - but you can assign the output of the tag to a variable that you can then loop over:
{% load custom_tags %}
{% for list in remind_lists %}
<h3>{{ list.title }}</h3>
{% get_list_items list user.username as list_items %}
{% for item in list_items %}
<p>{{ item.title }}</p>
{% endfor %}
{% endfor %}
# you can format the text or data in the function itself and return the same to the template
{% for list in remind_lists %}
<h3>{{ list.title }}</h3>
{{ list.id|get_list_items:authenticated_user }}
{% endfor %}
register = template.Library()
#register.simple_tag
def get_list_items(event_ins, authenticated_user):
# you can format the text or data here
return ...
hey guys i have trouble in display all post with same relatedid and related id comes from qna and i want to show all relatedid post to qna_post.html please help to fix it in django 1.11 python3
urls.py
from django.conf.urls import url, include
from django.views.generic import ListView, DetailView
from qna.models import Post
urlpatterns = [
url(r'^$', ListView.as_view(queryset=Post.objects.all().order_by("-date")[:25], template_name="qna.html")),
url(r'^(?P<pk>\d+)$', DetailView.as_view(model = Post, template_name= 'qna_post.html'))
]
qna_post.html
{% extends "header.html" %}
{% block content %}
{% for post in object_list %}
{% url page_item item.id %}
{% if post.relatedid == post.id %}
{% if post.PostType == 'q' %}
<h3>{{ post.Title }}</h3>
<h6>on {{ post.date }}</h6>
{{post.Body|safe|linebreaks}}
{% endif %}
{% if post.PostType == 'a' %}
<h3>{{ post.Title }}</h3>
<h6>on {{ post.date }}</h6>
{{post.Body|safe|linebreaks}}
{% endif %}
{% endif %}
{% endfor %}
{% endblock %}
qna.html
{% extends "header.html" %}
{% block content %}
{% for post in object_list %}
{% if post.PostType == 'q' %}
<h5>{{ post.date|date:"Y-m-d" }} {{ post.Title }} -<label> {{ post.Body|slice:":8" }}...</label></h5>
{% endif %}
{% endfor %}
{% endblock %}
I am using the following code to access the first message in Django template,
{% if messages %}
{% for message in messages %}
{% if forloop.first %}
{{ message }}
{% endif %}
{% endfor %}
{% endif %}
How I can achieve the same without using the for loop in a single statement.
Following should work:
{% if messages %}
{{ messages.0 }}
{% endif %}
clients_list
{'clients': [
{'id': 357995, 'value': 1.0},
{'id': 369743, 'value': 0.9}
]}
{% try %}
{% if clients_list %}
{% for client in clients_list %}
{% for user in client %}
{% raw user.id %}
{% raw user.value %}
{% end %}
{% end %}
{% end %}
{% except %}
{% end %}
Output expected:
357995
1.0
369743
0.9
The problem is that loop in template is wrong. How can i access the id and value?
This is a tornado template, but i think that is similar to django.
Update:
{% try %}
{% if clients_list %}
{% for client in clients_list %}
{% raw client %} // outputs the clients_list
{% for user in client %}
{% raw user %} outputs 'clients'
{% end %}
{% end %}
{% end %}
{% except %}
{% end %}
Here is the solution.
{% try %}
{% if clients_list %}
{% for client in clients_list %}
{% for user in client['clients'] %}
{% raw user['id'] %}
{% raw user['value'] %}
{% end %}
{% end %}
{% end %}
{% except %}
{% end %}
I'm trying to do something like:
{% for property in current_listing %}
{% for property_image in property.property_images.all %}
{% endfor %}
{% endfor %}
But I would like something like:
{% for property in current_listing %}
{% for property_image in property.property_images.**ORDER_BY('-order')[0]** %}
{% endfor %}
{% endfor %}
How can I do this?
If I understand what you want, you can try custom template filter:
from django import template
register = template.Library()
#register.filter
def get_first_ordered_by(queryset, order):
return queryset.order_by(order)[0]
Then on a template:
{% load my_tags %}
{% with image=property.property_images.all|get_first_ordered_by:'-order' %}
{{ image }}
{% endwith %}
Note, that you can not use {% for %} since result of get_first_ordered_by is not iterable.
You can add a method to you Model's class definition that returns the query you want, then cann that method from you template.