本文介绍了django表单下拉列表数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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

有人在其表单模板中使用javascript完成了此操作,这可以在Django中完成吗?

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

推荐答案

您正在寻找默认情况下呈现为select html元素的ChoiceField. https://docs.djangoproject.com/en/dev/ref/表单/字段/#choicefield

You're looking for a ChoiceField which renders as a select html element by default.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)])

这篇关于django表单下拉列表数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 23:24