如何使Django模型形式为只读? [英] How to make a Django Model form Readonly?
本文介绍了如何使Django模型形式为只读?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个Django表单名称 SampleForm。我用来接受用户输入的内容。现在,我想使用相同的表单在不同页面上向用户显示此信息。但是表格是可编辑的,我想使表格只读。有什么方法可以使整个表单变为只读状态?
I have a django form name "SampleForm". Which i use to take input from user. Now i want to use same form to show this information to user on a different page. But form is editable I want to make the form read only. Is there any way to make whole form Readonly ?
推荐答案
伪代码(未经测试):
class ReadOnlyFormMixin(ModelForm):
def __init__(self, *args, **kwargs):
super(ReadOnlyFormMixin, self).__init__(*args, **kwargs)
for key in self.fields.keys():
self.fields[key].widget.attrs['readonly'] = True
def save(self, *args, **kwargs):
# do not do anything
pass
class SampleReadOnlyForm(ReadOnlyFormMixin, SampleForm):
pass
这篇关于如何使Django模型形式为只读?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文