【问题标题】:How to get a template tag to auto-check a checkbox in Django如何获取模板标签以自动检查 Django 中的复选框
【发布时间】:2010-03-15 13:05:24
【问题描述】:

我正在使用 ModelForm 类为 ManyToManyField 生成一堆复选框,但我遇到了一个问题:虽然默认行为会自动选中相应的框(当我编辑对象时),但我可以'不知道如何在我自己的自定义模板标签中获取该信息。

这是我的模型中的内容:

from myproject.interests.models import Interest


class Node(models.Model):
    interests   = models.ManyToManyField(Interest, blank=True, null=True)


class MyForm(ModelForm):

    from django.forms import CheckboxSelectMultiple, ModelMultipleChoiceField

    interests = ModelMultipleChoiceField(
        widget=CheckboxSelectMultiple(), 
        queryset=Interest.objects.all(),
        required=False
    )

    class Meta:
        model = MyModel

在我看来:

from myproject.myapp.models import MyModel,MyForm

obj = MyModel.objects.get(pk=1)
f   = MyForm(instance=obj)

return render_to_response(
    "path/to/form.html", {
        "form": f,
    },
    context_instance=RequestContext(request)
)

在我的模板中:

{{ form.interests|alignboxes:"CheckOption" }}

这是我的模板标签:

@register.filter
def alignboxes(boxes, cls):
    """
        Details on how this works can be found here:
            http://docs.djangoproject.com/en/1.1/howto/custom-template-tags/
    """

    r = ""
    i = 0
    for box in boxes.field.choices.queryset:
        r += "<label for=\"id_%s_%d\" class=\"%s\"><input type=\"checkbox\" name=\"%s\" value=\"%s\" id=\"id_%s_%d\" /> %s</label>\n" % (
            boxes.name,
            i,
            cls,
            boxes.name,
            box.id,
            boxes.name,
            i,
            box.name
        )
        i = i + 1

    return mark_safe(r)

问题是,我这样做只是为了可以在这些框周围包裹一些更简单的标记,所以如果有人知道如何以更简单的方式实现这一点,我会全力以赴。我很高兴知道一种方法来访问是否应该选中一个框。

【问题讨论】:

    标签: python django templatetags


    【解决方案1】:

    在复选框的 input 标记中,您可以根据某些条件添加 checked 属性。假设您的 box 对象具有属性 checked,其值为“checked”或空字符串“”

    r += "<label for=\"id_%s_%d\" class=\"%s\"><input type=\"checkbox\" name=\"%s\" value=\"%s\" id=\"id_%s_%d\" %s /> %s</label>\n" % (
        boxes.name,
        i,
        cls,
        boxes.name,
        box.id,
        boxes.name,
        i,
        box.checked,
        box.name
    )
    

    【讨论】:

    • 就是这样,盒子对象没有“选中”属性。鉴于我必须使用的只是 {{ form.interests }},我正试图找出在哪里可以获得该值。我假设数据必须在那里,因为如果我在没有模板标签的情况下渲染它,则会自动选中这些框。
    【解决方案2】:

    结果是我正在寻找的值,列表中“检查”的元素不在field 中,而是form 对象的一部分。我重新设计了模板标签,使其看起来像这样,它正是我需要的:

    @register.filter
    def alignboxes(boxes, cls):
    
        r = ""
        i = 0
        for box in boxes.field.choices.queryset:
            checked = "checked=checked" if i in boxes.form.initial[boxes.name] else ""
            r += "<label for=\"id_%s_%d\" class=\"%s\"><input type=\"checkbox\" name=\"%s\" value=\"%s\" id=\"id_%s_%d\" %s /> %s</label>\n" % (
                boxes.name,
                i,
                cls,
                boxes.name,
                box.pk,
                boxes.name,
                i,
                checked,
                box.name
            )
            i = i + 1
    
        return r
    

    对于那些可能会跟进的人,请注意上面的 checked 值是在 boxes.form.initial[boxes.name] 中找到的

    【讨论】:

      猜你喜欢
      • 2016-04-25
      • 1970-01-01
      • 2020-01-01
      • 2011-11-11
      • 2021-02-01
      • 2011-09-09
      • 2013-05-18
      • 2013-10-08
      • 1970-01-01
      相关资源
      最近更新 更多