单独显示选中的项目,没有复选框 [英] showing the checked item alone without check box

查看:27
本文介绍了单独显示选中的项目,没有复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

class Actions(models.Model):
    report = models.ForeignKey(Report)
    action =  models.IntegerField('Action type')

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.

谢谢

推荐答案

基于 James 的回答.您可以将 PERSON_ACTIONS 移动到您的模型中并以表单形式导入.

Based on James's answer. You can move PERSON_ACTIONS to your model and import it in form.

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屋!

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