Django:在模板标签中使用模板中提供月份和年数作为参数 [英] Django: provide month and year number as parameter in a template for use in template tag

查看:208
本文介绍了Django:在模板标签中使用模板中提供月份和年数作为参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Django的初学者。
我发现这个Django 片段在我的网页上显示一个简单的日历。该功能需要3个可以在模板中提供的参数,如下所示:

  {%load calendar_tag%} 
< ; DIV>
< div>日历:< / div>
{%get_calendar for 2010 2010为日历%}
< table>
< tr>
< th> Mon< / th>
< th> Tue< / th>
< th> Wed< / th>
th th th th th th th th th th th th th th th th th th th Th
< th> Fri< / th>
  Sat< / th>
< th> Sun< / th>
< / tr>
{%在日历%}
< tr>
{%for week in week%}
< td> {{day.day}}< / td>
{%endfor%}
< / tr>
{%endfor%}
< / table>
< / div>

如何从服务器动态提供月份和年份的数量,例如:

  {%nowjS FYH:i%} 

不能在块标记中有块标记。
我很乐意使用这个简单的日历。
提前感谢

解决方案

我会假定您在视图中呈现模板时会传递参数,如下所示:

  def yourview(request,month,year):
return render_to_string(template.html {
month:month,
year:year,
})

然后相应地更改 get_calendar

  { %get_calendar的月份为日历%} 


I am a Django Beginner. I found this Django snippet to show a simple calendar on my web page. The function needed 3 parameters that one can provide within the template as follows:

{% load calendar_tag %}
<div>
<div>Calendar: </div>
{% get_calendar for 10 2010 as calendar %}
<table>
    <tr>
        <th>Mon</th>
        <th>Tue</th>
        <th>Wed</th>
        <th>Thu</th>
        <th>Fri</th>
        <th>Sat</th>
        <th>Sun</th>
    </tr>
    {% for week in calendar %}
    <tr>
        {% for day in week %}
        <td>{{ day.day }}</td>
        {% endfor %}
    </tr>
    {% endfor %}
</table>
</div>

How can I provide the number of the month and the year dynamically from the server, for example like:

{% now "jS F Y H:i" %}

It is not possible to have a block tag within a block tag. I would appreciate to use this simple calendar. Thanks in advance.

解决方案

I would presume you would pass the params in when rendering the template in your view, like so:

def yourview(request, month, year):
    return render_to_string("template.html", {
        "month": month,
        "year": year,
    })

and then change get_calendar accordingly:

{% get_calendar for month year as calendar %}

这篇关于Django:在模板标签中使用模板中提供月份和年数作为参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆