Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用html输入类型month在Django中显示年和月的值_Html_Django_Django Models_Django Views_Django Templates - Fatal编程技术网

使用html输入类型month在Django中显示年和月的值

使用html输入类型month在Django中显示年和月的值,html,django,django-models,django-views,django-templates,Html,Django,Django Models,Django Views,Django Templates,我有一个输入类型月,在这里我要求用户键入作业的开始月份和开始年份。但是,字段月份是可选的。像用户一样,只能指定起始年份。在我的模型中,我将start_month存储为CharField,其值为“一月”、“二月”。我将起始年存储为整数字段。我已经存储了这些值,甚至成功地检索了它们,这样,如果我执行{{job.start_month},它们就会显示在模板中 问题是,如果用户只能有年、月或年,如何通过输入类型“月”中的“值”属性显示该值 请帮助:((( 注意:我对Django表单不太满意:/ 以下是我

我有一个输入类型月,在这里我要求用户键入作业的开始月份和开始年份。但是,字段月份是可选的。像用户一样,只能指定起始年份。在我的模型中,我将start_month存储为CharField,其值为“一月”、“二月”。我将起始年存储为整数字段。我已经存储了这些值,甚至成功地检索了它们,这样,如果我执行{{job.start_month},它们就会显示在模板中

问题是,如果用户只能有年、月或年,如何通过输入类型“月”中的“值”属性显示该值

请帮助:((( 注意:我对Django表单不太满意:/

以下是我的代码片段:

<div class="form-group col-md-6">
    <label for="start">From:</label>
    <input style="margin-left: 5px;" type="month" class="start_date" value=" 
    {% if job.start_year and job.start_month %}{{ job.start_year }}-{{ 
    job.start_month_as_number }}{% else %} {{ job.start_year }}-{% endif %}" 
    name="start">
</div>

发件人:

将此添加到您的模型中:

@property
def start_month_as_number(self):
    if not self.start_month:
        return ""
    month_dict = {
        "January":"01",
        "February":"02",
        "March":"03",
        "April":"04",
        "May":"05",
        "June":"06",
        "July":"07",
        "August":"08",
        "September":"09",
        "October":"10",
        "November":"11",
        "December":"12",        
    }
        return month_dict[self.start_month]
然后在模板中可以这样打印:
{{job.start\u month\u as\u number}

为了安全起见,您还需要将choices参数添加到
start\u month
字段中

month_choices = ["January","February","March","April","May","June","July","August",September"","October", "November", "December"]

class StudentWorkExperience(models.Model):
...
    start_month = models.CharField(max_length=10, choices=month_choices, null=True, blank=True)

展示你的models@Sumithran我编辑了我的代码。有没有办法让月份名称变成一个数字?比如一月变成01?如果我硬编码月份,这个解决方案适用于一种情况number@E保罗,这一行在做什么{{工作。开始一个月{u编号}?通常,我知道可以通过job.start访问开始月份_month@EPaul,如果你能快速地告诉我这是将月份名称转换为数字,我会很高兴的。@E Paul okayy.N如果我没有月份,只有一年,情况会怎样?对不起,打扰你了,它将返回一个空字符串,没有月份会返回我不太明白你想干什么。