我正在尝试使用Django编写一个日历应用程序,而我似乎无法显示该月的日期。对于views.py
中的每月观看次数,我有:
from datetime import date, datetime, timedelta
import calendar
@login_required
def month(request, year, month, change=None):
"""Listing of days in `month`."""
year, month = int(year), int(month)
# apply next / previous change
if change in ("next", "prev"):
now, mdelta = date(year, month, 15), timedelta(days=31)
if change == "next": mod = mdelta
elif change == "prev": mod = -mdelta
year, month = (now+mod).timetuple()[:2]
# init variables
cal = calendar.Calendar()
month_days = cal.itermonthdays(year, month)
nyear, nmonth, nday = time.localtime()[:3]
lst = [[]]
week = 0
# make month lists containing list of days for each week
# each day tuple will contain list of entries and 'current' indicator
for day in month_days:
entries = current = False # are there entries for this day; current day?
if day:
entries = Entry.objects.filter(date__year=year, date__month=month, date__day=day)
if day == nday and year == nyear and month == nmonth:
current = True
lst[week].append((day, entries, current))
if len(lst[week]) == 7:
lst.append([])
week += 1
return render_to_response("cal/month.html", dict(year=year, month=month, user=request.user, month_days=lst, mname=mnames[month-1]))
对于年度模板,我有:
{% extends "cal/base.html" %}
{% block content %}
<a href="{% url cal.views.main year|add:'-3' %}"><< Prev</a>
<a href="{% url cal.views.main year|add:'3' %}">Next >></a>
{% for year, months in years %}
<div class="clear"></div>
<h4>{{ year }}</h4>
{% for month in months %}
<div class=
{% if month.current %}"current"{% endif %}
{% if not month.current %}"month"{% endif %} >
{% if month.entry %}<b>{% endif %}
<a href="{% url cal.views.month year month.n %}">{{ month.name }}</a>
{% if month.entry %}</b>{% endif %}
</div>
{% if month.n == 6 %}<br />{% endif %}
{% endfor %}
{% endfor %}
{% endblock %}
显示一年中的月份。但是当我点击月份时,它不显示日期。
对于每月模板,我有:
{% extends "cal/base.html" %}
{% block content %}
<a href= "{% url cal.views.month year month "prev" %}"><< Prev</a>
<a href= "{% url cal.views.month year month "next" %}">Next >></a>
<h4>{{ mname }} {{ year }}</h4>
<div class="month">
<table>
<tr>
<td class="empty">Mon</td>
<td class="empty">Tue</td>
<td class="empty">Wed</td>
<td class="empty">Thu</td>
<td class="empty">Fri</td>
<td class="empty">Sat</td>
<td class="empty">Sun</td>
</tr>
{% for week in month_days %}
<tr>
{% for day, entries, current in week %}
<!-- TD style: empty | day | current; onClick handler and highlight -->
<td class= {% if day == 0 %}"empty"{% endif %}
{% if day != 0 and not current %}"day"{% endif %}
{% if day != 0 and current %}"current"{% endif %}
{% if day != 0 %}
onClick="parent.location='{% url cal.views.day year month day %}'"
onMouseOver="this.bgColor='#eeeeee';"
onMouseOut="this.bgColor='white';"
{% endif %} >
<!-- Day number and entry snippets -->
{% if day != 0 %}
{{ day }}
{% for entry in entries %}
<br />
<b>{{ entry.creator }}</b>: {{ entry.short|safe }}
{% endfor %}
{% endif %}
</td>
{% endfor %}
</tr>
{% endfor %}
</table>
<div class="clear"></div>
</div>
{% endblock %}
我不认为这是我构建月度视图的方式的问题。相反,我认为这与我将年度模板与我的月度模板相关联的方式存在问题。我是Django的新手和一般的编程,所以如果有人能指出我正确的方向,我会非常感激。
编辑:
这是app/urls.py
中的urlconf:
from django.conf.urls import patterns, include, url
from cal.views import main
from cal.views import month
from cal.views import day
urlpatterns = patterns('',
(r'^(\d+)/$', main),
(r'', main),
(r'^month/(\d+)/(\d+)/(prev|next)/$', month),
(r'^month/(\d+)/(\d+)/$', month),
(r'^month$', month),
(r'^day/(\d+)/(\d+)/(\d+)/$', day),
)
Main处理年度视图的模板。
答案 0 :(得分:1)
所以我猜测main
是生成年/月列表的视图。麻烦的是,第二个正则表达式匹配一切,因为你没有锚定它。你需要这个:
(r'^$', main),
这样它只匹配开头和结尾之间没有任何内容的字符串 - 即空字符串。
答案 1 :(得分:0)
您没有渲染您在模板中使用的名为years
的对象{% for year, months in years %}