[PYTHON] How to convert DateTimeField format in Django

I want to format and display the date acquired by Django

At Django's models.py When using timezone.now () instead of datetime.now ()

date = models.DateTimeField ('date', default = timezone.now ())

Then the format is 2020-12-08 21:12:24.333404+00:00 become that way.

When you want to express something like 12/08/20 on a web application

date = models.DateTimeField(datetime.now().strftime('%m%d%y %H:%M'))

If you do

ValidationError [u"'12/08/2020' value has an invalid date format. It must be in YYYY-MM-DD format."]

Error is displayed.

Solutions

When displaying the date acquired in html, you can specify the format as follows.

<div>{{ item.ans_date|date:"m/d/y H:i" }}</div>

In this case, it will be displayed as "01/01/20 02:41" .

In addition, if you want to use 12h notation, do not add 0 at the beginning, or display the year in 4 digits, refer to the following page. https://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=olddocs#date

Have a good Django life!

Recommended Posts

How to convert DateTimeField format in Django
How to convert 0.5 to 1056964608 in one shot
How to reflect CSS in Django
How to easily convert format from Markdown
How to convert csv to tsv in CLI
How to delete expired sessions in Django
How to do Server-Sent Events in Django
[Tensorflowjs_converter] How to convert Tensorflow model to Tensorflow.js format
How to implement Rails helper-like functionality in Django
How to reflect ImageField in Django + Docker (pillow)
How to run some script regularly in Django
How to convert / restore a string with [] in python
How to get multiple model objects randomly in Django
How to convert floating point numbers to binary numbers in Python
How to use bootstrap in Django generic class view
Convert / return class object to JSON format in Python
How to use Decorator in Django and how to make it
How to convert Json file to CSV format or EXCEL format
How to develop in Python
How to reference static files in a Django project
How to write custom validations in the Django REST Framework
How to use Laravel-like ORM / query builder Orator in Django
How to output a document in pdf format with Sphinx
How to check ORM behavior in one file with django
How to update user information when logging in to Django RemoteUserMiddleware
[Django] How to give input values in advance with ModelForm
How to generate a query using the IN operator in Django
[Python] How to do PCA in Python
[Django] How to test Form [TDD]
How to use classes in Theano
How to write soberly in pandas
How to collect images in Python
Errors related to memcached in django
How to update Spyder in Anaconda
How to use SQLite in Python
How to get started with Django
How to kill processes in bulk
How to use Mysql in python
How to convert Tensorflow model to Lite
How to wrap C in Python
How to use ChemSpider in Python
How to use PubChem in Python
How to convert from .mgz to .nii.gz
How to run TensorFlow 1.0 code in 2.0
How to handle Japanese in Python
How to log in to Docker + NGINX
How to authenticate with Django Part 2
How to authenticate with Django Part 3
How to call PyTorch in Julia
Determine the date and time format in Python and convert to Unixtime
Convert PIL format images read from form with Django to base64 format
[Django] How to read variables / constants defined in an external file
[ROS2] How to describe remap and parameter in python format launch
How to deploy a Django app on heroku in just 5 minutes
How to return the data contained in django model in json format and map it on leaflet
How to do arithmetic with Django template
How to use calculated columns in CASTable
[Python] How to convert db file to csv
[Introduction to Python] How to use class in Python?
How to suppress display error in matplotlib
Convert xml format data to txt format data (yolov3)