python - 从 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)
解决方案
您无法在 django 模板中设置值,您的以下代码无效
{{ month = i.record_date.getMonth() }}
要获取月份,只需从 i.record_date 获取
{% if i.record_date.month == searchrecordmonth %}
推荐阅读
- angular - 选项卡内的 Angular Material mat 选项卡和主第一个选项卡名称未更改
- pentaho - 使用 PDI 的 Pentaho 报告输出组件出现错误
- java - 带有 RabbitMQ 生产者的 REST 端点
- javascript - 角度 6 等待回复
- google-cloud-dataflow - GCP Dataflow API 不评估在 dataprep 上编写的配方中调用的 now/today() 函数
- bash - 在 bash 变量上使用 cut 时出现“找不到命令”
- http - NMEA 数据的正确 Content-Type 标头?
- php - Symfony UniqueEntity 验证消息
- php - 无法加载请求的类:Phpmailer_library
- python - 去掉空方括号给出 ValueError: invalid literal for int() with base 10: