Is it possible to add days to the date in the template? - django

I have today's date in my template, which is in a loop. I want to add a counter to the cycle so that in the template I don't have today's date, but date + 1 day, date + 2 days, and so on.
{% for day_week_list in set_day_week %}
<div class="card mb-2">
<div class="card-header">
{{ day_week_list }} {{ day_data|date:'d.m' }}
</div>
<div class="card-body">

No, that's not possible. To add days to a date in HTML, you need to use a programming language such as JavaScript.

Related

Why does my time countdown return NaN when i format time in django template

I have been working on this app where i have a time countdown to event date, having a challenge working around the time format in django when i format the start datetime in django format{{ event.start|date:'D d M Y' }}" it return NaN in template, but when i format datetime like this "date: 2021-11-11T08:32:06+11:11" it works, I have search on here for a solution, but somehow the solution didn't work in my case.
HTML
#This works
<div class="items-center space-x-2 text-center grid grid-cols-4" uk-countdown="date: 2021-11-11T08:32:06+11:11">
<div class="bg-gray-100 rounded-md p-2 border shadow-inner">
<div class="uk-countdown-days text-lg font-bold"></div>
<div class="text-xs">DAYS </div>
</div>
<div class="bg-gray-100 rounded-md p-2 border shadow-inner">
<div class="uk-countdown-hours text-lg font-bold"></div>
<div class="text-xs">HOURS </div>
</div>
<div class="bg-gray-100 rounded-md p-2 border shadow-inner">
<div class="uk-countdown-minutes text-lg font-bold"></div>
<div class="text-xs">MIN </div>
</div>
<div class="bg-gray-100 rounded-md p-2 border shadow-inner">
<div class=" uk-countdown-seconds text-lg font-bold"></div>
<div class="text-xs">SEC </div>
</div>
</div>
#but this does not work, It returns NaN
<div class="items-center space-x-2 text-center grid grid-cols-4" uk-countdown="{{ event.start|date:'D d M Y' }}"">
Here is my model for event
class Event(models.Model):
start = models.DateTimeField(_('start'),db_index=True,default=datetime.now().replace(microsecond=0))
end = models.DateTimeField(_('end'), db_index=True,default=datetime.now().replace(microsecond=0))
My view for event
def event_main(request,pk):
event = get_object_or_404(Event, pk=pk)
ctx = {'event':event}
return render(request,'event/event_main.html',ctx)
The formats are completely different... In the first example, the value is presented in ISO format
uk-countdown="date: 2021-11-11T08:32:06+11:11"
in the second, you're formatting it
uk-countdown="{{ event.start|date:'D d M Y' }}""
which would give you
uk-countdown="Fri 11 Nov 2021""
you should probably use
uk-countdown="date: {{ event.start|date:"c" }}"
Note that I've added the date: prefix and removed the second " as well.
You also have an issue with your model defaults. This:
start = models.DateTimeField(_('start'),db_index=True,default=datetime.now().replace(microsecond=0))
end = models.DateTimeField(_('end'), db_index=True,default=datetime.now().replace(microsecond=0))
will give you the time the module is imported, which in production can be days before your model is saved...

unable to retrive data in crud operation

{%for x in read%}
{%if request.user == user%}
<div class="card " >
<h5>
<div class="card-header bg-info">
Dr.{{i.name}}
</div>
</h5>
<div class="card-body">
<h6 class="card-title ">Appointment Date : {{x.appoinment_date}}</h6>
<h6 class="card-title">Age : {{x.age}}</h6>
<h6 class="card-title">Digree : {{x.disease}}</h6>
<h6 class="card-title">Email : {{x.email}}</h6>
</div>
</div>
{%endif%}
{%endfor%}
i am using code above but i am getting data of all users instead of current user
I'm not familiar with your application, but it seems like the if-statement relates to some variable 'user', while in the table everything relates to 'x.something'. If what you're trying to do is filter for rows that are only related to the current user, you may want to check
{%if request.user == x.user%}

Concatenate string and UUID in Django Template

I am trying to concatenate the UUID of a record with a base URL to create a scannable QR code that will link to the direct record on the website. When trying to concatenate the two it fails and yields nothing.
The relevant part is device.id which is a UUID for the device. I've string |stringformat:"s" as well and that didn't work. I don't know what the best practice to do this is and am struggling.
<div class="row">
<div class="col-xs-12 text-center">
{% with "http://127.0.0.1:8000/ims/device/"|add:device.id as deviceurl %}
{% qr_from_text deviceurl size=25 %}
<p class="small text-center">{{deviceurl}}</p>
{% endwith %}
<p class="small text-center">{{ device.id }}</p>
</div>
</div>
Since the |add filter only works with two strings it cannot be used as a general answer. I created a custom |addstr filter and included it in the file which solved the problem.
How to concatenate strings in django templates?

set a variable in django template file, and update it after iteration

Trying to set variable in django template, and make a simple rule to update it after iteration. Here is my template:
{% for adv in advs %}
<div class="media-item big" style="top: 18%;left:{% cycle '304' '1078' %}px;">
<div class="media-item__tags">
{{ adv.year }}
{{ adv.payer}}
</div>
<div class="media-item__content">
<div class="media-item__background">
<div class="media-item__canvas">
<div class="media-item__canvas-background" style="background-image: url({{adv.image.url}})"></div>
</div>
<h2 class="topic white upcase fixed-size">{{ adv.name }}</h2>
Смотреть проект
</div>
</div>
</div>
In first div i need to make different 'left:' value. I want to make rule: after every iteration, value changes from base=304 to base+774 px. I tryed to do it somehow with {% cycle %} but it doesnt work for me, also tryed to set variables with {% with %} tag, but didnt find any information about how to update them.
You can set the style by multiplying the current counter from 0...n with 774 and add base value 304. For this, you'll need a custom template tag.
Create a templatetags directory in your app. Add an empty __init__.py and multiply_add.py.
multiply_add.py
from django import template
register = template.Library()
#register.simple_tag
def mul_add(a, b, base_value):
return (a * b) + base_value
template.html
{% load multiply_add %}
{% for adv in advs %}
<div class="media-item big" style="top: 18%;left:{% multiply_add forloop.counter0 774 304 %}px;">
<div class="media-item__tags">
{{ adv.year }}
{{ adv.payer}}
</div>
<div class="media-item__content">
<div class="media-item__background">
<div class="media-item__canvas">
<div class="media-item__canvas-background" style="background-image: url({{adv.image.url}})"></div>
</div>
<h2 class="topic white upcase fixed-size">{{ adv.name }}</h2>
Смотреть проект
</div>
</div>
</div>

django slice numbers in template

Is there a way to get multiple digits of a given number within a django template?
For example:
{{ some_num|get_digit:2 }}
will give you the second right most digit. For 1224531 it would be 3
Is there a way to get the last 3 digits or the first 5 digits? Like python's slicing?
something like:
{{ some_num|get_digits:2,5}}
There is a the "slice" template tag
https://docs.djangoproject.com/en/dev/ref/templates/builtins/#slice
It uses the same syntax as Python's list slicing.
Example:
{{ some_list|slice:":2" }}
in python this is equivalent to:
some_list[:2]
BTW your 2nd example would be "2:5" not "2,5"
NB. Python slicing works on any 'sequence'. Strings and lists are sequences. Numbers are not!
any extra filter that converts the number into a string before slicing will work. I used these variants:
{{ some_num|slugify|slice:"2:5" }}
and
{{ some_num|stringformat:"d"|slice:"5:10" }}
{{1234567|make_list|slice:'2:5'|join:''}}
Stefano's answer is on the right track. You need a pre-processing step to turn your number into a list, and a post-processing step to merge that list back into string.
You just need to write code as follow for slicing :-
{{valueformoney|slice:"0:4"}}
{% for cloth in valueformoney|slice:"0:4" %}
<div class="product h-100 w-100 border rounded ">
<div class="img__container">
<img src="{{cloth.cloth_image.url}}" alt="" />
</div>
<div class="product__bottom">
<div class="price">
<span class="text-danger"> <del>{% min_price cloth as result %} {{ result|rupee}}</del></span>
<span>{% discount_price cloth as result %}{{result|rupee}}</span>
<span class="float-right badge p-3 badge-info">Save {{cloth.cloth_discount}}% </span>
</div>
<h3 class="p-4">{{cloth.cloth_name}}</h3>
<div class="button">
See More
</div>
</div>
</div>
{% endfor %}