0

我有下面列出的项目myproject和应用程序。myapp当我尝试 url 时/add,我收到此错误:

Page not found (404)
Request Method: GET
Request URL:    http://localhost:8000/add/
Using the URLconf defined in myproject.urls, Django tried these URL patterns, in this order:
^$
The current URL, add/, didn't match any of these.

设置配置:

ROOT_URLCONF = 'myproject.urls'

我的项目/urls.py

from django.conf.urls import patterns, include, url

urlpatterns = patterns('',
    url(r'^$', include('myapp.urls')),
)

我的应用程序/urls.py

from django.conf.urls import patterns, include, url

urlpatterns = patterns('myapp.views',
    url(r'^$', 'main'),
    url(r'^add/', 'foo'),
)

我的应用程序/views.py

from django.shortcuts import render

def main(request):
    return render(request, "main.html", {'test': 1})

def foo(request):
    return render(request, "add.html")

模板/main.html

{{ test }}

模板/add.html

{{ test }}

怎么了?

4

1 回答 1

3

这一行:

url(r'^$', include('myapp.urls')),

意味着只有完全空的 URL 将被传递给 myapp 包含。你是这个意思:

url(r'^', include('myapp.urls')),
于 2012-09-28T19:09:59.150 回答