django和python的新手。尝试获取一些示例日历代码并运行,但遇到URL映射问题。当我尝试运行管理页面(或页面),我得到:django URL映射?
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
,这里是我的URL模式:
(r"^(\d+)/$", "main"),
(r"", "main"),
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
url(r'^admin/', include(admin.site.urls)),
我很困惑,虽然,因为它似乎对我来说,函数“main”确实存在于views.py中,如下所示。任何帮助是极大的赞赏:
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)))
尝试这样做,建议从@Alasdair,但现在我得到一个“问题加载页面“错误在浏览器中,当我查看dev服务器的日志时,它显示它正在尝试”GET“一个不存在的页面:'”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' – mb52089
你抓住所有的url'r“”'引起重定向循环。我在我的回答中解释了更多。 – Alasdair