0

我正在尝试使用 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' %}">&lt;&lt; Prev</a>
<a href="{% url cal.views.main year|add:'3' %}">Next &gt;&gt;</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" %}">&lt;&lt; Prev</a>
<a href= "{% url cal.views.month year month "next" %}">Next &gt;&gt;</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 和一般编程的新手,所以如果有人能指出我正确的方向,我将不胜感激。

编辑:

这是我的urlconf app/urls.py

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 处理年度视图的模板。

4

2 回答 2

1

所以我猜这main是产生年/月列表的视图。问题是第二个正则表达式匹配所有内容,因为你没有锚定它。你需要这个:

(r'^$', main),

这样它只匹配开始和结束之间没有任何内容的字符串 - 即空字符串。

于 2012-07-06T06:49:24.230 回答
0

您没有渲染years您在模板中使用的对象{% for year, months in years %}

于 2012-07-06T09:55:08.543 回答