我是初学者,所以请回答一些愚蠢的问题
如果我在本地主机中删除了突出显示的行1,2,3(+静态行),它将显示正常的django主页,但是在添加这些突出显示的行之后,它将显示错误
Page not found (404)
Request Method: GET
Request URL: http://localhost:8000/
Using the URLconf defined in portfolio.urls, Django tried these URL patterns, in this order:
admin/
^media/(?P<path>.*)$
The empty path didn't match any of these.
You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
但是管理页面加载没有问题
#urls.py file
from django.contrib import admin
from django.urls import path
from django.conf import settings **<----- 1**
from django.conf.urls.static import static **<----- 2**
urlpatterns = [
path('admin/', admin.site.urls),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) ** <--- 3**
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
答案 0 :(得分:0)
您正在尝试访问应用程序的根页面。
您的主要urls.py应该如下所示
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = = [
path('admin/', admins.site.urls),
path('', include("exampleapp.urls"),
]
创建另一个应用,该应用将使用python manage.py startapp exampleapp处理您的根页面
在exampleapp / urls.py
中from django.urls import path
from . import views
app_name = "exampleapp"
urlpatterns = [
path('', views.index, name="index)
]
在exampleapp / views.py
from django.shortcuts import render
def index(request):
return render(request, "index.html", {})
基本上,您没有在应用程序的根urls.py中指定应用程序的“ /”路由,也未指定应用程序的索引。您需要这样做,以上是一个示例。
如果尚未添加,请在settings.py中添加:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')