rfj001
rfj001

Reputation: 8498

"Missing Positional Arguments"

I am trying to render an ajax response from a view, but I am getting an error that the view is missing positional arguments.

This is the error message I am getting

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'

Here is my view

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)

And here is my 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'
    ),
)

You can see that the url being passed is /schedules/calendar/2014/10/1, which has the year and month parameters passed (2014 and 10, respectively), as well as the user_id parameter (1). Why is python/django saying that I am missing parameters?

Upvotes: 0

Views: 1436

Answers (1)

karthikr
karthikr

Reputation: 99620

Since you are using positional arguments, django urls are expecting the named group patterns to have the same name as that of the argument (to the view)

So, change

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

to

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

in your urls.py

Upvotes: 1

Related Questions