如何显示Django表单中的不可编辑字段 [英] How can I show non editable field in django form

查看:655
本文介绍了如何显示Django表单中的不可编辑字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字段为 datetime = models.DateTimeField(auto_now_add = True)的模型。在根据此模型创建的表单中,我不需要编辑此字段,但需要显示它。不会自动显示具有 auto_now_add = True 的字段。显示此字段的最佳方法是什么?

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?

推荐答案

假设您通过了实例创建表单对象时,可以使用以下模板在其中访问它:

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" %}

这篇关于如何显示Django表单中的不可编辑字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆