首页 > 解决方案 > 从 Django 模板中的 DateField 中提取月份

问题描述

想从模板中提取月份和年份

模板_1.html

   <form method = 'POST'>
      <input type ='month' name = 'searchmonth'>
      <input type = 'submit' value = 'Search'>
   </form>

模板_2.html

   {% for i in record %}
   {% if i.record_patient_number == number %}
   {{ month = i.record_date.getMonth() }}

   {% if month == searchrecordmonth %}
     *something*


   {% endif %}
   {% endif %}
   {% endfor %}

所有变量都包含在views.py中

在这个搜索月份中无法提取并且 record_date 保存在 models.py 中并作为

    record_date = models.DateField(blank = True , null = True)

标签: pythondjango

解决方案


您无法在 django 模板中设置值,您的以下代码无效

{{ month = i.record_date.getMonth() }}

要获取月份,只需从 i.record_date 获取

{% if i.record_date.month == searchrecordmonth %}

推荐阅读