thundera
thundera

Reputation: 53

How can I show non editable field in django form

I've got a model with field datetime = models.DateTimeField(auto_now_add=True). In form created from this model I don't need to edit this field but I need to show it. The fields with auto_now_add=True automatically isn't shown. What is the best way to show this field?

Upvotes: 5

Views: 2282

Answers (2)

Michał Bielawski
Michał Bielawski

Reputation: 76

Assuming you pass an instance when creating 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, though, the smartest thing to do would be to use current time:

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

Upvotes: 4

mariodev
mariodev

Reputation: 15604

You can display the instance value when editing form like this:

{{ form.instance.datetime }}

Upvotes: 1

Related Questions