Remove duplicate values in django forloop - django

i have some dates in a list and I want to display all days in the list using forloop at Django template. what I want is to remove duplicate days in it.
here is the sample code
{% for part_time_day in part_time %}
{{part_time_day.start_time|date:"D"}},
{% endfor%}
it will display all the days like "Sun, Mon, Fri, Sun" etc but I want to remove duplicate days so one day can display only once

Django template has a ifchanged tag:
{% for part_time_day in part_time %}
{% ifchanged %} {{part_time_day.start_time|date:"D"}}{% endifchanged %},
{% endfor%}
It looks if the content has changed from previous iteration. So it will work better if you have ordered by start_time before.

Related

Double loop with django Templates

so I have this matrix sent to a view
[[6.197, 6.156, 6.165, 6.164, 4.741], [6.191, 6.106, 6.175, 6.132, 4.741], [6.158, 6.137, 6.137, 6.133, 4.741]]
and a list containing dates
["11-12-2016","12-12-2016","13-12-2016"]
and I want to format them with Template to look like this
[["11-12-2016",6.197, 6.156, 6.165, 6.164, 4.741]
["12-12-2016",6.191, 6.106, 6.175, 6.132, 4.741]
....]
Iam using this code :
{% for date in dates %}
{% with forloop.counter0 as i %}
,["{{date}}"{% for item in selling.i %} ,{{item}} {% endfor %}]
{% endwith %}
{% endfor %}
and it doesn't work , but when I replace i with 0,1.. the second loop works fine on one list
{% for item in selling.i %}
This isn't going to work -- the template will look for an attribute or index literally equal to "i", and not the value of that variable.
The Django template language actively discourages using too much logic in the template, and this is an example of something you can't do.
So create the lists as you want them in Python, and pass those to the template. E.g. in Python
combined = [[str(date)] + sell for date, sell in zip(dates, selling)]
And in the template
{% for row in combined %}
[{{ row|join:","|safe }}],
{% endfor %}

Django how to list model objects date wise

I have a model from which I need to list all the objects from a given time period.
For example: 10-05-2014 to 10-12-2014
And then I need to display the objects date wise. Meaning first display objects created on the start_date(10-05-2014) up until end_date(10-12-2014)
For example:
10-05-2014:
objects list for that day
11-05-2014:
objects list for that day
and so on until end_date
Query:
MyModel.objects.unsettled.filter(
created_on__range=[start_date, end_date]
)
But my problem is how do I list the query set in increasing order by date wise in my template. So that all the objects created on same date will be shown under that date. In short I want to display the list in sections divided by date.
MyModel.objects.unsettled.filter(created_on__range=[start_date, end_date]).order_by("created_on"). But it will just sort the list. How do I group the results.??
Use the {% ifchanged %} template tag:
{% for obj in obj_list %}
{% ifchanged %}
<h2>{{ obj.created_on|date }}</h2>
{% endifchanged %}
<div>{{ obj.name }}</div>
{% endfor %}
Another (slightly more complex) option is to use the {% regroup %} tag.

getting multiple paginator

If I include paginator, then its printing multiple paginator on same page. because I am including in forloop. How do I solve this problem? please help
----------------------
{% autopaginate recipes %}
{% for recipe in recipes %}
{{ forloop.counter }}
{% endfor %}
-----------
here is my full code.
{% with followers=current_user.get_profile.get_following.all %}
{% for follower in followers %}
{% with recipes=follower.recipe_set.all %}
{% for recipe in recipes %}
{{ forloop.counter }}
{% endfor %}
{% endwith %}
{% endfor %}
{% endwith %}
Result - > 1 2 3 1 2 3 ...... 50. It should be 53. So I can easily use paginator
thanks
the doc string in the setup.py file of django-pagination mentions the below,
Decide on a variable that you would like to paginate, and use the autopaginate tag on that variable before iterating over it. This
could take one of two forms (using the canonical object_list
as an example variable):
{% autopaginate object_list %}
This assumes that you would like to have the default 20 results per page. If you would like to specify your own amount of
results per page, you can specify that like so:
`{% autopaginate object_list 10 %}`
Note that this replaces object_list with the list for the current page, so you can iterate over the object_list like you normally would.
Now you want to display the current page and the available pages, so somewhere after having used autopaginate, use the paginate
inclusion tag:
{% paginate %}
This does not take any arguments, but does assume that you have already called autopaginate, so make sure to do so first.
That's it! You have now paginated object_list and given users of
the site a way to navigate between the different pages--all without
touching your views.
So, where are you using paginate tag after the autopaginate? You actually need not loop through recipes, autopaginate should do that for you wherever you call paginate.

django DateTimeField list records by day

Hay, i have a field in one of my models which saves the creation date of an object
created_on = models.DateTimeField(blank=False, auto_now_add=True)
This works as expected.
In my templates i want to list objects like this
June 15
{{ objects here which was created on June 15 }}
June 14
{{ objects here which was created on June 14 }}
etc
Any idea how i would go about doing this?
Thanks in advance.
First in the view, make sure your objects are ordered by the 'created_on' field.
object_list = MyObjs.objects.all().order_by('created_on')
I believe you should then be able to use the following code in your template:
{% regroup object_list by created_on|date:"Y-m-d" as objects_by_day %}
{% for day in objects_by_day %}
{{day.list.0.created_on|date:"M d"}}
{% for obj in day.list %}
{{obj}}
{% endfor %}
{% endfor %}
This makes use of the regroup template tag, grouping the items by day, creating a list of objects per day. The date itself is output by using the 'date' template filter to reformat the created_on field of the first item in that date's list.
[Note: I have not tested this!]
The place for this is in the template, not the view.
You want the ifchanged template filter, which you use like this:
<h1>Archive for {{ year }}</h1>
{% for entry in entries %}
{% ifchanged %}<h3>{{ entry.date|date:"F" }}</h3>{% endifchanged %}
<p>{{ entry.date|date:"j" }} - {{ entry.title }}</p>
{% endfor %}
In this example, every time the month changes, a heading showing the month will be printed.
ifchanged can do some relatively complex things (e.g. checking if multiple variables have all changed), and can have an optional else block - see the docs for more.
you can for example generate list of dates(by day on given interval) in your view ordered from min to max, and get all objects in this interval. Then create templatetag that takes 2 arguments queryset and date,
and filter records which created(or something else) by your date.

Django templates - Regrouping by a string parameter

I have the following code in one of my Django templates that I want to refactor:
{% ifequal sort_type "set" %}
{% regroup cards by set as grouped %}
{% endifequal %}
{% ifequal sort_type "rarity" %}
{% regroup cards by rarity as grouped %}
{% endifequal %}
It does work, but it's really ugly and I want to make it more like this:
{% regroup cards by sort_type as groupedcards %}
But this doesn't work (it just puts them all in a single group called None.) From the documentation, I think it might be trying a dictionary lookup (i.e., calling card["set"] instead of card.set).
Is there a good way to do this in the template, or should I move the regrouping out into the Python code using itertools?
Ticked in Django bugtracker related to this problem.