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 %}

1年の月が表示されます。しかし、月をクリックしても、日は表示されません。

月次テンプレートの場合、次のようになります。

{% 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が年/月のリストを生成するビューだと推測しています。問題は、それをアンカーしていないため、2番目の正規表現がすべてに一致することです。あなたはこれを必要とします:

(r'^$', main),

開始と終了の間に何もない文字列、つまり空の文字列のみに一致するようにします。

于 2012-07-06T06:49:24.230 に答える
0

yearsテンプレートで使用するというオブジェクトをレンダリングしていません{% for year, months in years %}

于 2012-07-06T09:55:08.543 に答える