Django URL 映射?
django 和 python 新手。尝试启动并运行一些示例日历代码,但在 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
这是我的网址模式:
(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)))
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)))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
错误消息告诉您
main
函数不存在于cal
模块中 - 这是正确的,它存在于cal.views
中> 模块。如果您将 url 模式更改为以下内容,它应该可以工作:
我已注释掉上面的
r""
url,因为它是一个包罗万象的 url。它出现在登录 URL 模式上方,因此您的main
视图正在处理日志 URL/accounts/login/
。main
视图使用login_required
装饰器,导致重定向循环。The error message is telling you that the
main
function does not exist in thecal
module -- that is correct, it exists in thecal.views
module.If you change your url patterns to the following, it should work:
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 yourmain
view is handling the log url/accounts/login/
. Themain
view uses thelogin_required
decorator, causing a redirect loop.阿拉斯代尔的回答是正确的。
我只想添加以下内容: https://docs .djangoproject.com/en/1.3/intro/tutorial03/#simplifying-the-urlconfs
您可以这样声明它以方便:):
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 :) :