How to show non editable field in django form

I have a model with a datetime = models.DateTimeField(auto_now_add=True) . In the form created with this model, I do not need to edit this field, but I need to show it. Fields with auto_now_add=True are not automatically displayed. What is the best way to show this field?

+4
source share
2 answers

Assuming you pass instance when creating the form object, you can access it in your template using:

 {{ form.instance.datetime|date:"Y/m/d H:i:s" }} 

If you don't have an instance, the smartest thing is to use the current time:

 {% now "Y/m/d H:i:s" %} 
+2
source

You can display the instance value when editing the form as follows:

 {{ form.instance.datetime }} 
+1
source

Source: https://habr.com/ru/post/1502824/


All Articles