use*_*353 1 python django date
嗨我需要在我的django模板文件中检查每个月的第10个,有人可以告诉我如何能够在每个月的10号之前显示一些数据并在10日之后隐藏
有几点需要了解:
因此,在我看来,最简单的方法是:
所以,我们的观点看起来像:
def sample_view(request, *args, **kwargs):
from datetime import date
current_date = date.today()
current_day = int(current_date.day) # force a cast to an int, just to be explicit
return render_to_response("template", {'day_of_the_month': current_day })
Run Code Online (Sandbox Code Playgroud)
在我们的模板中,我们会这样做:
{% if day_of_the_month > 10 %}
<!-- Stuff to show after the 10th -->
{% else %}
<!-- Stuff to show before the 10th -->
{% endif %}
Run Code Online (Sandbox Code Playgroud)
出于安全原因,这确实会根据日期向您的模板公开内容.您永远不应该基于if/then检查在模板中隐藏安全的东西.
如果你需要做额外的检查,你可以直接传入current_date
,并检查current_date.day
数字10,而不是在那一天特别传递.它不那么明确,但更通用和灵活.
归档时间: |
|
查看次数: |
2348 次 |
最近记录: |