Linux django 表单下拉数字列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8859504/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 04:04:22  来源:igfitidea点击:

django form dropdown list of numbers

pythondjangolinux

提问by krisdigitx

i am new to django and i want to make a simple form, according to the doc i can make a form using forms module from django

我是 django 的新手,我想制作一个简单的表单,根据文档,我可以使用 django 的表单模块制作表单

from django import forms

class CronForm(forms.Form):
    name = forms.CharField(max_length=100)
    email = forms.EmailField(required=False, label='Your e-mail address')
    message = forms.CharField(widget=forms.Textarea)

    def clean_message(self):
        message = self.cleaned_data['message']
        num_words = len(message.split())
        if num_words < 4:
            raise forms.ValidationError("Not enough words!")
        return message

what i want to know is how to create a dropdown list of days in month i.e from 1 to 31?

我想知道的是如何创建一个月中的天数下拉列表,即从 1 到 31?

some have done it using javascript in their form template, can this be done in django?

有些人在他们的表单模板中使用 javascript 完成了它,这可以在 django 中完成吗?

采纳答案by Yuji 'Tomita' Tomita

You're looking for a ChoiceFieldwhich renders as a selecthtml element by default. https://docs.djangoproject.com/en/dev/ref/forms/fields/#choicefield

您正在寻找默认ChoiceField呈现为selecthtml 元素的 。 https://docs.djangoproject.com/en/dev/ref/forms/fields/#choicefield

class CronForm(forms.Form):
    days = forms.ChoiceField(choices=[(x, x) for x in range(1, 32)])

回答by Arthur Kamau

you can start by choosing to render the fields manualy. A simple example:-

您可以从选择手动渲染字段开始。一个简单的例子:-

{% for field in form %}
        <div class="fieldWrapper">
            {{ field.errors }}
            {{ field.label_tag }} {{ field }}
            {% if field.help_text %}
                <p class="help">{{ field.help_text|safe }}</p>
            {% endif %}
        </div>
    {% endfor %}

This will render the form without any styling then you have to apply the relevent class to create a drop down list(copied from w3c dropdown css):-

这将呈现没有任何样式的表单,然后您必须应用相关类来创建下拉列表(从 w3c 下拉 css 复制):-

 <div class="dropdown">
    <button class="btn btn-default dropdown-toggle" type="button" id="menu1" data-toggle="dropdown">Tutorials
    <span class="caret"></span></button>
    <ul class="dropdown-menu" role="menu" aria-labelledby="menu1">
      <li role="presentation"><a role="menuitem" tabindex="-1" href="#">HTML</a></li>
      <li role="presentation"><a role="menuitem" tabindex="-1" href="#">CSS</a></li>
      <li role="presentation"><a role="menuitem" tabindex="-1" href="#">JavaScript</a></li>
      <li role="presentation" class="divider"></li>
      <li role="presentation"><a role="menuitem" tabindex="-1" href="#">About Us</a></li>
    </ul>
  </div>
</div>

combining the two :-

结合两者:-

<div class="dropdown">
        <button class="btn btn-default dropdown-toggle" type="button" id="menu1" data-toggle="dropdown">Days
            <span class="caret"></span></button>
        <div class="fieldWrapper">
            <ul class="dropdown-menu" role="menu" aria-labelledby="menu1">
                {% for field in form %}
                    <li class="fieldWrapper">
                        {{ field.errors }}
                        {{ field.label_tag }} {{ field }}
                        {% if field.help_text %}
                            <p class="help">{{ field.help_text|safe }}</p>
                        {% endif %}
                    </li>
                {% endfor %}
            </ul>
        </div>
    </div>