I am trying to display time in 10:34 PM format but MySQL server has it in 22:34:00 format. I tried to do {{ invoice['time'].strftime("%I:%M %p") }} but I am getting error saying jinja2.exceptions.UndefinedError: 'datetime.timedelta object' has no attribute 'strftime'
I also tried to convert it to string then just get the first 5 character hoping to give at least 22:34 format but it also failed. {{ invoice['time']|string[:5] }}
How to achieve this? The MySQL stores it in time and not datetime.
Related
I undrestand that this question is repeated, but unfortunately I cannot find any answer.
My impression is that Django already takes care of converting server time to local user as long as I have
TIME_ZONE = 'UTC'
USE_TZ = True
in my settings.
Even more if the db is postgresql that setting also doesn't matter and every thing will be still converted.
however I tried all the followings:
{% load tz %}
{{ obj.date }}
{{ obj.date|localtime }}
{{ obj.date | timezone:"Canada/Mountain" }}
and only last one works and the rest gives me UTC time. Last one also is not useful as the time would be only correct for users in that zone.
I was wondering if I am missing anything here.
I have a very simple test model:
class TimeObject(models.Model):
date = models.DateTimeField(auto_now=True)
My impression is that Django already takes care of converting server time to local
Unfortunately that is incorrect.
As discussed here, there's no automatic way of knowing what the user's timezone is. You need to figure that out in some other way (it could be saved as a user setting, for example). Then you need to activate() that timezone. Once you do, Django will perform the conversion.
I have defined one custom tag which is working fine in templates.
Like
{% get_setting "DATE_FORMAT_UI" %}
Above statement is returning correct value in template.
Now i want to use the same in a filter like this -
{{extra_info.to_date|date: '{% get_setting "DATE_FORMAT_UI" %}' }}
But this is giving error in parsing.
I tried in different ways of using quotes for the {% get_setting "DATE_FORMAT_UI" %}
But every time bad luck.
So could any body help me in solving this. I want to pass date format in filter . That date format is saved into config file. but how to pass that value dynamically in filter.
The trick is to first assign this to a variable (here myformat), and then use that variable:
{% get_setting 'DATE_FORMAT_UI' as myformat %}
{{extra_info.to_date|date:myformat }}
Im trying to display the month name of the datetime object on a django template. This is how Im sending the date:
{
..
'today': timezone.localtime(timezone.now())
..
}
On the template Im doing this, I have tried what this question has: (Django) how to get month name?
But still I'm not getting the desired result, maybe there's a difference with timezone.locatime() vs datetime.now()?:
{{ today|date:"F" }}
However Im only getting and empty blank space where whe month name should be. Can anyone help me correct my error?
I want to get the current date for a specific timezone in my Django app, irrespective of the server's timezone. I save the user's timezone in the database. I'll then use that in the following function:
def current_date(zone):
utc = timezone.now()
tz = pytz.timezone(zone)
return utc.astimezone(tz).date()
print(current_date('Pacific/Auckland')) #prints 2016-05-30
print(current_date('Africa/Accra')) #prints 2016-05-29
It seems to work, but working with timezones seems complex and I'm wondering if something can go wrong with this approach?
It looks fine as long as getting to the value in view itself is what you want. Since the date/time print is an aspect of presentation you probably may not want to do it in the view code rather in the template using something like below:
{% load tz %}
{{ object.datetime_field|timezone:request.user.timezone }}
assuming you are storing the user's timezone selection in the user model.
This may be a simple one. But I could not reason it out. I am using Django 1.7.2
I have a model where there is only one row of data in the table.
Case 1 : objects.all()
info = ModelName.objects.all()
I get the data but when I try to put it in a template, it is not getting displayed.
{% for item in info %}
{{ item.name }}
{% endfor %}
Case 2 : objects.get()
info = ModelName.objects.get()
I get the data and it is getting displayed in template.
{{ info.name }}
Questions:
Could anyone explain why case 1 is not working?
And in case 2 using get() without any pk value is fine?
Case 2 works fine, because the arguments for the get() function are optional. If you don't provide anything, it will match against every record. Since you table only has one, it raises no exception.
See the docs: https://docs.djangoproject.com/en/1.7/ref/models/querysets/#get
Your case 1 should be working, though. Check if your info is in your context for the template.