I want to enter date and time in my admin site with
date = models.DateTimeField()
But I dont want to have the seconds in my view. Is it possible to display the time like this:
10:45 instead of 10:45:00 ?
Yes you can:
https://docs.djangoproject.com/en/dev/ref/forms/fields/#datetimefield
input_formats
A list of formats used to attempt to convert a string to a valid datetime.datetime object.
If no input_formats argument is provided, the default input formats are:
Thats if you want to save as well with no seconds, If you only want to change how its displayed in your templates your can use the date template filter:
https://docs.djangoproject.com/en/1.6/ref/templates/builtins/#date
Related
Time format
"2018-12-13T05:20:06.427Z"
django providing time zone in above format when i am fetching data from database using ORM query.
In my model field is in below way.
models.DateTimeField(auto_now=True, blank=True,null=True)
How can i convert it into "24 feb 2018" like this
Apart from #Sosthenes Kwame Boame answer, you can use strftime for formatting.
import datetime
time = datetime.datetime.now().strftime('%d %b %Y')
Out[13]: '13 Dec 2018'
Instead of passing datetime module to time variable, you should pass your model field's value.
If you want to learn more about format type then you can visit the documentation page.
I am guessing you want to display this on the frontend?
You need to do this in your template:
{{ object_name.datefield_name|date:'j b Y' }}
So you call the datefield and render it with the '|date' tag with a format assigned ':'FORMAT''.
Learn more about the date tag, along with the various formats here: https://docs.djangoproject.com/en/2.1/ref/templates/builtins/#date
I want to sort by DateTimeField ignoring one hour.
Because in my ordination I need
Person.objects.order_by('-date_joined', 'full_name')
Sorting by name has no effect because it is a Timefield , but I wanted to data.
Depending on your Django version (1.9+), this should work:
Person.objects.order_by ('-date_joined__date','full_name')
Otherwise you can use .extra to cast into date field:
Person.objects.extra(
select={'joined_date': 'DATE(myapp_person.date_joined)'},
order_by=['-joined_date', 'full_name'],
)
i want to allow users to be able to choose between am and pm with my django timefield
Currently if I enter:
11:00 AM
, i get a form error: "Enter a valid time."
If I enter:
11:00
the form validates with no problem.
I also tried:
class RemindersForm(forms.ModelForm):
remdinder = forms.TimeField(input_formats='%H:%M %p',)
class Meta:
model = NotificationPreference
fields = (
'reminder',
)
This changes the input format to:
11:00:00
and still gives me the above validation error.
What am I doing wrong?
I was searching for a similar answer and I didn't find a suitable one for models.TimeField, so, the easiest solution that I found for doing this site wide would be to setting in your settings.py the following global variable:
TIME_INPUT_FORMATS = ['%I:%M %p',]
I hope this helps someone.
To use other formats see:
https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior
To get a 12hr time format you would use the following:
For input (This is the list of formats Django uses in validation):
field = TimeField(input_formats=('%I:%M %p',...,...))
For output (This is the format Django will use to display time values):
field = TimeField(widget=TimeInput(format='%I:%M %p'))
The %I indicates a 12 hour clock format whereas the %H indicates a 24 hour clock format.
Additionally, the default list of input_formats can be found in each locales formats.py file. Django uses the first format in the input_formats list as the default output format for time fields.
I think you can do some thing like this in forms.py
field = DateTimeField(input_formats='%H:%M %p',...)
According to the Django documentation, and python's datetime docs, it should work if you change the time input settings to:
TIME_INPUT_FORMATS = ('%I:%M %p',)
I have a field which will represent the start time of an event and I am using the Django DateTimeField for this.
This field is mandatory but sometimes the users will only know the start date and not the time.
Is there anyway to make the time part optional and keep the date part mandatory?
Maybe you should try to separate date from time. There are DateField and TimeField for that.
Example for use at the views or models:
You can use function strptime to show the datetime field any formats.
from datetime import datetime
datetime.now().strftime('%Y-%m-%d')
# print string '2013-06-25'
Example for use at the templates:
you can use templatetag date
{{ datetime_field|date:"Y-m-d" }}
I'm trying to display the expiry date of a bonus from within a Django template. At the moment the opening_date is stored as a datefield and we store the bonus term as an integerfield. Unfortunately just trying to add the bonus term to the opening date fails and the furthest I have got so far is:
{{product_form.instance.opening_date|add:product_form.instance.bonus_term}}
I have tried just adding it to the month but unfortunately I need the whole date returned to display.
For a better idea of what I want is say the opening date was 01/01/2012 and the bonus term was 12, I want to display the expiry date of 01/01/2013. I realise this is probably better off being in the database but due to the way it has been previously set up there is a large amount of existing data that wouldn't have it.
Thanks.
I think that, for your scenario, the most elegant solution is to create a model method in your model that calcule expire date, then call the method in template:
In model:
class product(models.Model):
opening_date = ...
bonus_term = ...
def expire_date( self ):
return self.opening_date + timedelta( days = self.bonus_term )
In template:
{{product_form.instance.expire_date}}
I'm sure that you will call this method in more other lines of your code.