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