当前 URL 与其中任何一个都不匹配,include() 给出问题
The current URL did not match any of these, include() giving problems
我决定为每个应用制作一个 urls.py
。但是,当我 include
网址时,我 运行 遇到了问题。我不知道是什么问题,因为我做对了一切。我的站点与 Django 1.9 教程具有相同的结构。我在根目录下有 template
文件夹,在 myapp
中还有一个 template
目录,就像 template\myapp\
一样。 home.html
和 profile.html
在 myapp\template\myapp\
只有 myapp
中的 url 出现问题,因此既不会呈现 home.html
也不会呈现 profile.html
,这会给出
Using the URLconf defined in mysite.urls, Django tried these URL
patterns, in this order:
^$ [name='index']
^myapp/
^admin/
The current URL, profile, didn't match any of these.
错误。我可以通过直接在主站点 mysite
中编写所有 urls
,通过导入它们的 views
让它们工作,但我宁愿让事情更有条理,让每个应用程序都有自己的 urls.py
.我也注册了 myapp
并把 myapp
放在了 INSTALLED_APPS
下
我的应用:查看
from django.shortcuts import render
def home(request):
return render(request, 'myapp/home.html')
def profile(request):
return render(request, 'myapp/profile.html')
我的应用程序网址:
from django.conf.urls import url
from .import views
app_name = 'myapp'
urlpatterns = [
url(r'^home/$', views.home, name='home'),
url(r'^profile/$', views.profile, name='profile'),
]
我的网站网址:
from django.conf.urls import include, url
from django.contrib import admin
from .import views
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^myapp/', include('myapp.urls')),
url(r'^admin/', admin.site.urls),
]
您似乎只是 /profile,但您没有将其定义为 URL,您有 /myapp/profile.
我决定为每个应用制作一个 urls.py
。但是,当我 include
网址时,我 运行 遇到了问题。我不知道是什么问题,因为我做对了一切。我的站点与 Django 1.9 教程具有相同的结构。我在根目录下有 template
文件夹,在 myapp
中还有一个 template
目录,就像 template\myapp\
一样。 home.html
和 profile.html
在 myapp\template\myapp\
只有 myapp
中的 url 出现问题,因此既不会呈现 home.html
也不会呈现 profile.html
,这会给出
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
^$ [name='index'] ^myapp/ ^admin/
The current URL, profile, didn't match any of these.
错误。我可以通过直接在主站点 mysite
中编写所有 urls
,通过导入它们的 views
让它们工作,但我宁愿让事情更有条理,让每个应用程序都有自己的 urls.py
.我也注册了 myapp
并把 myapp
放在了 INSTALLED_APPS
我的应用:查看
from django.shortcuts import render
def home(request):
return render(request, 'myapp/home.html')
def profile(request):
return render(request, 'myapp/profile.html')
我的应用程序网址:
from django.conf.urls import url
from .import views
app_name = 'myapp'
urlpatterns = [
url(r'^home/$', views.home, name='home'),
url(r'^profile/$', views.profile, name='profile'),
]
我的网站网址:
from django.conf.urls import include, url
from django.contrib import admin
from .import views
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^myapp/', include('myapp.urls')),
url(r'^admin/', admin.site.urls),
]
您似乎只是 /profile,但您没有将其定义为 URL,您有 /myapp/profile.