我在view.py中有to_date函数,它从url http://www.example.com/2014/05/03/传递日期; to_date生成指向所请求URL的链接,并明天生成明天的链接tomorrow_date。此外,还有一些逻辑从url处理日期:
def to_date(request, year, month, day):
day = int(day)
month = int(month)
year = int(year)
current_date = '{year}{month}{day}'.format(year=year,
month=date.month,
day=date.day)
next_date = datetime(year, month, day) + timedelta(days=1)
tomorrow_date = '{year}{month}{day}'.format(year=next_date.year,
month=next_date.month,
day=next_date.day)
# Any logic:
# ...do_something
template = loader.get_template('content.html')
context = RequestContext(request, {
'current_date': current_date,
'tomorrow_date': tomorrow_date,
#'do_something': do_something,
})
return HttpResponse(template.render(context))
UPD:
我的content.html包含标签:
{% extends 'base.html' %}
{% block content %}
<div class='content'>
{{ do_something }}
</div>
<ul class='menu'>
<li><a href='{{ current_date }}'>Current date</a></li>
<li><a href='{{ tomorrow_date }}'>Tomorrow date</a></li>
</ul>
{% endblock %}
我将链接移至menu.html:
{% extends 'base.html' %}
{% block menu %}
<ul class='menu'>
<li><a href='{{ current_date }}'>Current date</a></li>
<li><a href='{{ tomorrow_date }}'>Tomorrow date</a></li>
</ul>
{% endblock %}
要移动指向menu.html的链接,他们应该分开链接 来自content.html。函数views.to_date从url(它是任意的)获取数据并生成数据和链接并给出模板content.html,当我删除了menu.html的链接时,我不知道如何创建templatetag所以他从url获取数据喜欢功能to_date(请求,年,月,日)并给予响应menu.html。