【问题标题】:showing the checked item alone without check box单独显示选中的项目而不显示复选框
【发布时间】:2013-07-13 04:33:10
【问题描述】:

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')

打印.html

{{ actionform.as_p}}

PersonActionsForm 包含带有多选复选框的项目。 在报表注册页面,用户可以选择任意一项或多项。选中的项以整数值保存在模型中。

因为我正在渲染整个表单,所以它会显示带有选中和未选中项目的整个表单。

在打印页面中,我只想单独显示选中的项目而不显示复选框。

如何在 django 中做到这一点。

谢谢

【问题讨论】:

  • 关于这个问题的任何更新。
  • 为什么不直接用css来控制呢?隐藏未选中的所有内容并仅显示选中的内容
  • @karthikr,选项来自 django 表单,在 db 中保存检查项目的值而不是描述(如 int)。是否可以使用 css 来完成。
  • 是的..您可以使用 jquery 检测它,并添加一个 css 类或删除这些节点。我并不是说这是最干净的方法
  • 如果你给我发一个示例链接,我会很容易。

标签: django django-models django-forms django-templates django-views


【解决方案1】:

基于詹姆斯的回答。您可以将PERSON_ACTIONS 移动到您的模型并以表单形式导入。

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   
})

...并在模板中渲染:

{% for action in actions %}
    {{ action.action_as_text }}
{% endfor %}

希望这会有所帮助。

【讨论】:

  • 我尝试手动将 froms 选项插入模型中,但没有显示选中的项目
  • 嗯,您能补充一下您是如何使用视图+模板代码打印出结果的吗?
  • 正是我的想法,obj 应该是从views.py 传递的操作对象。请查看gist.github.com/H25/cc0d50541ed824fb52aa
  • 嘿,你真的在​​views.py 中传递了action 对象吗?你能在模板中做{{ action }} 来调试吗?
  • 是的,我得到了这样的 db 对象 [, ]。
【解决方案2】:

您不应将表单用于非编辑显示目的。相反,在你的类上创建一个方法:

from forms import PERSON_ACTIONS
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)

然后您可以在模板中执行{{ obj.action_as_text }} 并获取您想要的文本。请注意,在PERSON_ACTIONS 数组中使用整数可能会更常见(那么您就不需要在action_as_text 中调用str。)

【讨论】:

  • 啊,循环引用。在不了解您的代码的情况下,尝试将 dict(PERSON_ACTIONS) 移动到 action_as_text 中并将导入放到文件底部。这不是最有效的方法,但它应该有效。或更多 PERSON_ACTIONS 到另一个模块中,这样您就可以在不拉入所有 incident.forms 的情况下导入它。
  • @JamesAylett 你能看看这个SO问题stackoverflow.com/questions/18592136/…
猜你喜欢
  • 2017-10-22
  • 2018-04-23
  • 1970-01-01
  • 1970-01-01
  • 2013-09-14
  • 2019-12-31
  • 2019-02-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多