Django URL 映射?

发布于 2024-12-13 13:34:41 字数 1903 浏览 3 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

把人绕傻吧 2024-12-20 13:34:41

错误消息告诉您 main 函数不存在于 cal 模块中 - 这是正确的,它存在于 cal.views 中> 模块。

如果您将 url 模式更改为以下内容,它应该可以工作:

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

我已注释掉上面的 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 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.

謸气贵蔟 2024-12-20 13:34:41

阿拉斯代尔的回答是正确的。
我只想添加以下内容: https://docs .djangoproject.com/en/1.3/intro/tutorial03/#simplifying-the-urlconfs

您可以这样声明它以方便:):

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

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'),
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文