如何在表单的显示中包含不可编辑的信息? [英] How to include uneditable information on a form's display?
问题描述
我正在使用django表单集,其中包含指定用户友好首选项的表单。
I am using a django formset containing forms that specify user friendship preferences.
我的表单字段为:
siteuser_id = forms.IntegerField(widget=forms.HiddenInput())
subscribed = forms.BooleanField(required=False)
ally = forms.BooleanField(required=False)
enemy = forms.BooleanField(required=False)
目标是显示一个人的所有朋友以及该人在游戏中的状态。
The goal is to display all of a person's friends and that person's status within the game.
当我在表单集中显示表单时,我想在友谊旁边显示该人的昵称(昵称不是唯一的,或者我将使用它代替siteuser_id)对那个人的偏爱。
When I display the forms in the formset, I'd like to display the nickname (nicknames are not unique or I would just use it instead of siteuser_id) of the person alongside the friendship preference for that person.
我尝试将用户名设置为表单字段,但这使其可编辑,我只希望它显示在表中,而不是可编辑。
I tried making username a form field, but that makes it editable, and I just want it to display within the table, not be editable.
帮助?
推荐答案
听起来好像您不一定需要表单-您可以只需将对象中的数据传递到模板即可。
It sounds like you don't necessarily want a form - you could just pass the data in objects to your template.
但是,如果您确实想使用表单来执行此操作,则可以设置一个自定义的 widget
表示表单字段,并子类 Widget
。参见 https://docs.djangoproject.com/en/dev/ref/forms / widgets / 。
However, if you do want to do this with a form, you could set a custom widget
for the form field, and subclass Widget
. See https://docs.djangoproject.com/en/dev/ref/forms/widgets/.
这篇关于如何在表单的显示中包含不可编辑的信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!