如何使 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屋!
查看全文