Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 629
thundera Avatar asked Sep 18 '13 13:09

thundera


People also ask

What is read only in django?

Instead, django-read-only uses always installed database instrumentation to inspect executed queries and only allow those which look like reads.

What is form Is_valid () in django?

The is_valid() method is used to perform validation for each field of the form, it is defined in Django Form class. It returns True if data is valid and place all data into a cleaned_data attribute.

What is unbound form in django?

Bound and unbound forms If it's bound to a set of data, it's capable of validating that data and rendering the form as HTML with the data displayed in the HTML. If it's unbound, it cannot do validation (because there's no data to validate!), but it can still render the blank form as HTML.

How do you make a field non mandatory in django admin?

The simplest way is by using the field option blank=True (docs.djangoproject.com/en/dev/ref/models/fields/#blank).


2 Answers

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" %}
like image 62
Michał Bielawski Avatar answered Oct 11 '22 07:10

Michał Bielawski


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

{{ form.instance.datetime }}
like image 1
mariodev Avatar answered Oct 11 '22 05:10

mariodev