1

Newbie to django and python. Trying to get some example calendar code up and running but having problems with URL mapping. When I try to run the admin page (or any page), I get:

ViewDoesNotExist at /

Tried main in module cal. Error was: 'module' object has no attribute 'main'

Request Method:     GET
Request URL:    http://127.0.0.1:8000/
Django Version:     1.3.1
Exception Type:     ViewDoesNotExist

and here are my url patterns:

(r"^(\d+)/$", "main"),
(r"", "main"),
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
url(r'^admin/', include(admin.site.urls)),

I am confused though, because it appears to me that the function "main" does exist in views.py, as shown below. Any help is greatly appreciated:

import time
from django.contrib.auth.decorators import login_required
from django.http import HttpResponseRedirect, HttpResponse
from django.shortcuts import get_object_or_404, render_to_response

from dbe.cal.models import *

mnames = "January February March April May June July August September October November December"
mnames = mnames.split()


@login_required
def main(request, year=None):
"""Main listing, years and months; three years per page."""
# prev / next years
if year: year = int(year)
else:    year = time.localtime()[0]

nowy, nowm = time.localtime()[:2]
lst = []

# create a list of months for each year, indicating ones that contain entries and current
for y in [year, year+1, year+2]:
    mlst = []
    for n, month in enumerate(mnames):
        entry = current = False   # are there entry(s) for this month; current month?
        entries = Entry.objects.filter(date__year=y, date__month=n+1)

        if entries:
            entry = True
        if y == nowy and n+1 == nowm:
            current = True
        mlst.append(dict(n=n+1, name=month, entry=entry, current=current))
    lst.append((y, mlst))

return render_to_response("cal/main.html", dict(years=lst, user=request.user, year=year,
                                               reminders=reminders(request)))
0

2 Answers 2

3

The error message is telling you that the main function does not exist in the cal module -- that is correct, it exists in the cal.views module.

If you change your url patterns to the following, it should work:

(r"^(\d+)/$", "cal.views.main"),
# (r"", "cal.views.main"),

I have commented out the r"" url above, because it is a catch all url. It appears above your pattern for the login url, so your main view is handling the log url /accounts/login/. The main view uses the login_required decorator, causing a redirect loop.

Sign up to request clarification or add additional context in comments.

Comments

1

Alasdair's answer is correct. I just want to add a bonus from : https://docs.djangoproject.com/en/1.3/intro/tutorial03/#simplifying-the-urlconfs

You can declare it this way for more convenience :) :

urlpatterns = patterns('cal.views',
                       (r'^(\d+)/$', 'main'),
                       (r'', 'main'),
)

2 Comments

tried this and suggestion from @Alasdair, but now I get a "problem loading page" error in the browser, and when I look at the dev server's log it shows that it's trying to "GET" a page that doesn't exist: '"GET /accounts/login/?next=/ HTTP/1.1" 302 0 [02/Nov/2011 14:14:42] "GET /accounts/login/?next=/accounts/login/%3Fnext%3D/ HTTP/1.1" 302 0 [02/Nov/2011 14:14:42] "GET /accounts/login/?next=/accounts/login/%3Fnext%3D/accounts/login/%253Fnext%253D/ HTTP/1.1" 302 0'
Your catch all url r"" is causing a redirect loop. I've explained more in my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.