当我尝试导航到我的django应用程序的管理部分时,我收到一个未找到的错误:
项目/ urls.py
from django.conf.urls import include, url
from django.contrib import admin
from app.views import View1, View2
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
url(r'^$', View1.as_view(), name="view_1"),
url(r'^another_view/(?P<pk>\d+)$', View2.as_view(), name="view_2"),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
应用程序/ urls.py
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
(r'^/', include('project.urls')),
]
项目/ settings.py
....
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
STATICFILES_DIRS = ( os.path.join(BASE_DIR, 'app/static'),)
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
MEDIAFILES_DIRS = ( os.path.join(BASE_DIR, 'app/media'),)
....
当我尝试导航到localhost:8000 / admin时,出现以下错误:
Page Not Found (404)
Using the URLconf defined in app.urls, Django tried these URL patterns, in this order:
^$ [name='view_1']
^another_view$ [name='view_2']
^media\/(?P<path>.*)$
The current URL, admin, didn't match any of these.
答案 0 :(得分:2)
您的应用和项目网址错误。
项目网址应包含管理员的映射,并包含应用网址;应用程序URL依次应包含应用程序视图的特定映射。
静态的东西应该仍然在项目网址中;虽然在任何情况下你都不需要它。