2

我有一个项目,其根 url conf 内容是:

 from django.conf.urls import patterns, include, url
import funnytest
urlpatterns = patterns(
url(r'^funnytest/', include('funnytest.urls')),
url(r'^helloworld/', funnytest.views.hello),
)

funtest是这个项目的一个app,在funnytest中我写了一个模块urls.py来配置这个app的请求:

from django.conf.urls import patterns, include, url
from views import *
urlpatterns = patterns(
url(r'^hello/$', hello),
)

当我访问 localhost/funnytest/hello/ 时,返回一个 dispath 错误,表示没有这样的模式

当我访问 localhost/helloworld 时,它运行良好。

呢,应该如何配置~

4

1 回答 1

5

如果您查看模式函数的定义:

def patterns(prefix, *args):
    pattern_list = []
    for t in args:
        if isinstance(t, (list, tuple)):
            t = url(prefix=prefix, *t)
        elif isinstance(t, RegexURLPattern):
            t.add_prefix(prefix)
        pattern_list.append(t)
    return pattern_list

您会看到模式在 url 模式列表之前有一个参数“前缀”。

在两个文件中尝试以下操作: 添加一个空字符串作为模式的第一个参数。

from django.conf.urls import patterns, include, url
import funnytest
urlpatterns = patterns(
    '',
    url(r'^funnytest/', include('funnytest.urls')),
    url(r'^helloworld/', funnytest.views.hello),
)

from django.conf.urls import patterns, include, url
from views import *
urlpatterns = patterns(
    '',
    url(r'^hello/$', hello),
)
于 2012-09-10T12:19:47.647 回答