【问题标题】:how to create check box and display it in templates如何创建复选框并将其显示在模板中
【发布时间】:2013-04-11 04:57:16
【问题描述】:

forms.py 是

class ShowlivereportForm(forms.Form):
    livereport = forms.BooleanField()

如何创建 django 复选框

我不知道如何创建一个显示复选框的模板,谁能给我一个例子

【问题讨论】:

    标签: python django django-models checkbox django-forms


    【解决方案1】:

    您可以在模板中使用 django 的自动 as_p 函数,或者您可以手动编写模板(以更好地控制其属性)。

    对于自动你需要在你的模板中:

    {{ form.as_p }}
    

    对于手册,您应该使用以下内容:

    <input type="checkbox" id="livereport" name="livereport" {% if form.livereport.value %}checked="checked"{% endif %}>
    <label for="livereport">Show live report</label>
    

    【讨论】:

    • 我用“{{ form.as_p }}”做了它没有呈现输出
    • @user2086641 如果您将form 作为render 函数的参数传递,它会起作用。查看此页面docs.djangoproject.com/en/dev/topics/forms 并查看示例。
    • 将经历并实施
    猜你喜欢
    • 1970-01-01
    • 2018-11-21
    • 1970-01-01
    • 2021-10-14
    • 1970-01-01
    • 2017-11-05
    • 1970-01-01
    • 2013-11-14
    • 1970-01-01
    相关资源
    最近更新 更多