Django Multiplechoicefield不在页面上显示 [英] Django multiplechoicefield not rending on page

查看:42
本文介绍了Django Multiplechoicefield不在页面上显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取一个包含名称和月份的表格,并将返回具有该名称和月份的学生(出勤系统).我正在为表单使用MultipleChoiceField,但它不会在浏览器中呈现.我也在使用Materialize CSS.我该如何解决这个问题?我也在手动呈现表单字段.

I'm trying to get a form that takes in a name and month and will return students with that name and month (attendance system). I'm using a MultipleChoiceField for the form but it does not render in the browser. I am also using Materialize CSS. How do I sort this issue? I'm also rendering the form fields manually.

forms.py:

class attendenceFinder(forms.Form):
    months = ((1, 'JAN'),
             (2, 'FEB'),
             (3, 'MAR'),
             (4, 'APR'),
             (5, 'MAY'),
             (6, 'JUN'),
             (7, 'JUL'),
             (8, 'AUG'),
             (9, 'SEP'),
             (10, 'OCT'),
             (11, 'NOV'),
             (12, 'DEC')
    )    

    name = forms.CharField()
    month = forms.MultipleChoiceField(widget=forms.SelectMultiple, choices=months)

attendence.html:

attendence.html:

<form action="/get_attendence/", method="post">
<div class="container">
        {{ form.non_field_errors }}
    <div class="fieldWrapper">
        {{ form.name.errors }}
        <label for="{{ form.name.id_for_label }}">Name</label>
        {{ form.name }}
    </div>
    <div class="fieldWrapper">
        {{ form.month.errors }}
        <label for="{{ form.month.id_for_label }}">{{form.month}}
            <span>{{ form.month.label }}</span>
        </label>
    </div>
    <button class="waves-effect waves-light btn-large lime accent-2 black-text" type="submit" name="action">Submit
    <i class="material-icons right">send</i>
    </div>
</form>

页面源:

   <div class="fieldWrapper">

        <select name="month" required id="id_month" multiple>
  <option value="1">JAN</option>

  <option value="2">FEB</option>

  <option value="3">MAR</option>

  <option value="4">APR</option>

  <option value="5">MAY</option>

  <option value="6">JUN</option>

  <option value="7">JUL</option>

  <option value="8">AUG</option>

  <option value="9">SEP</option>

  <option value="10">OCT</option>

  <option value="11">NOV</option>

  <option value="12">DEC</option>

</select>
        <span>Month</span>

    </div>

浏览器中的表单:

名称字段呈现良好,但月份"多项选择却不正确.

The name field renders fine but the Month multiple choice doesn't.

推荐答案

Materialize CSS希望看到带有class ="browser-default"的标签.但是,Django表单不会添加该类.因此,必须在forms.py中传递以下内容:

Materialize CSS expects to see tags with a class="browser-default". However Django forms do not add the class. So the following must be passed in forms.py:

class attendenceFinder(forms.Form):
    months = ((1, 'JAN'),
              (2, 'FEB'),
              (3, 'MAR'),
              (4, 'APR'),
              (5, 'MAY'),
              (6, 'JUN'),
              (7, 'JUL'),
              (8, 'AUG'),
              (9, 'SEP'),
              (10, 'OCT'),
              (11, 'NOV'),
              (12, 'DEC')
    )
 month = forms.MultipleChoiceField(choices=months, widget=forms.Select(
            choices=months,
            attrs={'class': 'browser-default'}))

所有功劳归于此链接:

https://github.com/Dogfalo/materialize/issues/4904

这篇关于Django Multiplechoicefield不在页面上显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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