单独显示检查项目而不复选框 [英] showing the checked item alone without check box
问题描述
forms.py
PERSON_ACTIONS = (
('1', '01.Allowed to rest and returned to class'),
('2', '02.Contacted parents /guardians'),
('3', '02a.- Unable to Contact'),
('4', '02b.Unavailable - left message'),)
class PersonActionsForm(forms.ModelForm):
action = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple(), choices=PERSON_ACTIONS, required=False, label= u"Actions")
models.py
models.py
class Actions(models.Model):
report = models.ForeignKey(Report)
action = models.IntegerField('Action type')
print.html
print.html
{{ actionform.as_p}}
PersonActionsForm包含项多核复选框。
在报告注册页面中,用户可以选择任何一个或多个项目。检查的项目以整数值保存在模型中。
The PersonActionsForm contains the items with multichoice checkbox. In report registration page,the user can select any one or more item.The checked items are saved in models as integer values.
由于我正在渲染整个表单显示整个表单,带有已检查和未选中的项目。
Since i am rendering the whole form it is showing the entire form with checked and unchecked item.
在打印页面中,我只想单独显示已检查的项目,而无需复选框。
In print page,i want to show only the checked item alone without checkbox.
如何在django中执行此操作。
How to do this in django.
谢谢
推荐答案
根据詹姆斯的答案。您可以将 PERSON_ACTIONS
移动到模型中并以表单导入。
Based on James's answer. You can move PERSON_ACTIONS
to your model and import it in form.
models.py:
models.py:
PERSON_ACTIONS = (
('1', '01.Allowed to rest and returned to class'),
('2', '02.Contacted parents /guardians'),
('3', '02a.- Unable to Contact'),
('4', '02b.Unavailable - left message'),
)
PERSON_ACTIONS_DICT = dict(PERSON_ACTIONS)
class Actions(models.Model):
report = models.ForeignKey(Report)
action = models.IntegerField('Action type')
def action_as_text(self):
return PERSON_ACTIONS_DICT.get(str(self.action), None)
forms.py:
from .models import PERSON_ACTIONS
class PersonActionsForm(forms.ModelForm):
action = forms.MultipleChoiceField(
widget=forms.CheckboxSelectMultiple(),
choices=PERSON_ACTIONS,
required=False,
label= u"Actions"
)
获取 views.py
中的操作:
actions = Actions.objects.filter(....)
return render(request, 'your_template.html', {
.....
'actions': actions
})
...并在模板中呈现:
... and render it in template:
{% for action in actions %}
{{ action.action_as_text }}
{% endfor %}
希望这有帮助。
这篇关于单独显示检查项目而不复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!