Django管理URL路由问题

时间:2018-04-03 22:29:56

标签: python django url

我一直在尝试将名为farm的Django应用程序合并到我当前的项目中,但由于某种原因localhost:8000/admin/在我尝试合并时不再路由到管理页面。我的urls.py文件如下:

主要应用:urls.py

from django.contrib import admin
from django.urls import include, path

urlpatterns = [
    path('admin/', admin.site.urls),
    path('farms/', include('farms.urls', namespace='farms'))
]

农场应用:urls.py

from django.urls import path
from . import views


app_name = 'farms'
urlpatterns = [
    path('', views.index, name='index'),
    path('profile/', views.profile, name='profile'),
    path('form/', views.profile_form, name='profile_form'),
    path('about/', views.about, name='about'),
]

每当我尝试转到localhost:8000/admin时,Django会尝试转到localhost:8000/admin/profile_form并产生以下错误:

Environment:


Request Method: GET
Request URL: http://localhost:8000/admin/

Django Version: 2.0.2
Python Version: 3.5.2
Installed Applications:
['farms.apps.FarmsConfig',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'bootstrap4']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']

Template loader postmortem
Django tried loading these templates, in this order:

Using engine django:
    * django.template.loaders.app_directories.Loader: /mnt/d/Projects/hawaiian-soil/farms/templates/admin/profile_form.html (Source does not exist)
    * django.template.loaders.app_directories.Loader: /mnt/d/Projects/hawaiian-soil/dep/lib/python3.5/site-packages/django/contrib/admin/templates/admin/profile_form.html (Source does not exist)
    * django.template.loaders.app_directories.Loader: /mnt/d/Projects/hawaiian-soil/dep/lib/python3.5/site-packages/django/contrib/auth/templates/admin/profile_form.html (Source does not exist)
    * django.template.loaders.app_directories.Loader: /mnt/d/Projects/hawaiian-soil/dep/lib/python3.5/site-packages/bootstrap4/templates/admin/profile_form.html (Source does not exist)

编辑:我显然是通过切换urls.py中路径的顺序解决了这个问题,如下所示:

urlpatterns = [
    path('farms/', include('farms.urls', namespace='farms'))
    path('admin/', admin.site.urls),

]

我不知道为什么这会解决这个问题。这只是Django的url命名空间工作方式的一部分,还是有另一个原因?

0 个答案:

没有答案