我有下面列出的项目myproject
和应用myapp
。当我尝试网址/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'
from django.conf.urls import patterns, include, url
urlpatterns = patterns('',
url(r'^$', include('myapp.urls')),
)
from django.conf.urls import patterns, include, url
urlpatterns = patterns('myapp.views',
url(r'^$', 'main'),
url(r'^add/', 'foo'),
)
from django.shortcuts import render
def main(request):
return render(request, "main.html", {'test': 1})
def foo(request):
return render(request, "add.html")
{{ test }}
{{ test }}
有什么问题?
答案 0 :(得分:3)
这一行:
url(r'^$', include('myapp.urls')),
表示只有完全空的网址会传递给myapp include。你的意思是:
url(r'^', include('myapp.urls')),