“缺少位置论证”

时间:2014-10-23 19:39:10

标签: python django django-views

我正在尝试从视图中呈现ajax响应,但是我收到一个错误,即视图缺少位置参数。

这是我收到的错误消息

Internal Server Error: /schedules/calendar/2014/10/1/
Traceback (most recent call last):
  File "/blahblahblah/django/core/handlers/base.py", line 111, in get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
TypeError: month_view() missing 2 required positional arguments: 'year' and 'month'

这是我的观点

def month_view(
    request, 
    year, 
    month, 
    template='swingtime/monthly_view.html',
    user_id=None,
    queryset=None
):
    '''
    Render a tradional calendar grid view with temporal navigation variables.

    Context parameters:

    today
        the current datetime.datetime value

    calendar
        a list of rows containing (day, items) cells, where day is the day of
        the month integer and items is a (potentially empty) list of occurrence
        for the day

    this_month
        a datetime.datetime representing the first day of the month

    next_month
        this_month + 1 month

    last_month
        this_month - 1 month

    '''
    year, month = int(year), int(month)
    cal         = calendar.monthcalendar(year, month)
    dtstart     = datetime(year, month, 1)
    last_day    = max(cal[-1])
    dtend       = datetime(year, month, last_day)

    # TODO Whether to include those occurrences that started in the previous
    # month but end in this month?
    if user_id:
        profile = get_object_or_404(UserProfile, pk=user_id)
        params['items'] = profile.occurrence_set
    queryset = queryset._clone() if queryset else Occurrence.objects.select_related()
    occurrences = queryset.filter(start_time__year=year, start_time__month=month)

这是我的urls.py

from django.conf.urls import patterns, url

from .views import (
    CreateSessionView, CreateListingsView, SessionsListView,
    month_view, day_view, today_view

)

urlpatterns = patterns('',
    url(r'^create-session/$',
        CreateSessionView.as_view(), name="create_session"),
    url(r'^create-listings/(?P<session>\d+)/$', CreateListingsView.as_view(),
        name = 'create_listings'),
    url(r'^my-sessions/$', SessionsListView.as_view(), name="session_list"),
    url(
        r'^(?:calendar/)?$', 
        today_view, 
        name='today'
    ),
    url(
        r'^calendar/(\d{4})/(0?[1-9]|1[012])/(?P<user_id>\d+)/$', 
        month_view, 
        name='monthly-view'
    ),

    url(
        r'^calendar/(\d{4})/(0?[1-9]|1[012])/([0-3]?\d)/(?P<user_id>\d+)/$', 
        day_view, 
        name='daily-view'
    ),
)

您可以看到传递的网址是/ schedules / calendar / 2014/10/1,其中传递了年份和月份参数(分别为2014年和10年),以及user_id参数(1)。为什么python / django说我缺少参数?

1 个答案:

答案 0 :(得分:1)

由于你使用位置参数,django urls期望named group patterns与参数(到视图)具有相同的名称

所以,改变

calendar/(\d{4})/(0?[1-9]|1[012])/(?P<user_id>\d+)/

calendar/(?P<year>\d{4})/(?P<month>0?[1-9]|1[012])/(?P<user_id>\d+)/

urls.py