Django 404 Error: path doesn't go down urls.py
Django 404 Error: path doesn't go down urls.py
所以我正在尝试为我的 Django 项目的主页制作一个 index.html。我制作了一个名为页面的应用程序。
请注意模板文件夹是空的(移动 index.html 到模板没有任何作用)。 admin.py 和 models.py 也不包含任何内容。
我这样配置了模板设置:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join(BASE_DIR, 'templates'),
],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
在我的根项目文件夹中,urls.py 看起来像这样。
from django.contrib import admin
from django.urls import path
from django.conf.urls import include
# from pages.views import home_view
urlpatterns = [
path('homepage/', include('pages.urls')),
path('admin/', admin.site.urls),
path('explorer/', include('explorer.urls')),
]
在页面应用程序的 views.py 文件中:
from typing import ContextManager
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def index(request):
# Render the HTML template index.html
return render(request, 'index.html', context=ContextManager)
在页面应用程序(不是项目根文件夹)中,urls.py 看起来像这样(我自己创建了这个文件):
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
和index.html:
<!DOCTYPE html>
<title>Test</title>
<p>Test</p>
这是完整的文件结构。
这是我 运行 服务器时发生的情况:
其中 http://127.0.0.1:8000/ 是本地主机的根目录。
index.html 应该在模板文件夹中。
pages/templates/index.html
这是因为您的根 urls 没有 ''
:
的任何内容
urlpatterns = [
path('homepage/', include('pages.urls')),
path('admin/', admin.site.urls),
path('explorer/', include('explorer.urls')),
]
我猜你想 http://127.0.0.1:8000/
转到索引,这就是你写这个的原因:
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
但是使用此配置,索引将在 http://127.0.0.1:8000/homepage/
上。如果你想将其设置为 /
,请将根 url 配置更改为:
urlpatterns = [
path('admin/', admin.site.urls),
path('explorer/', include('explorer.urls')),
path('', include('pages.urls')),
]
最后,您需要将 html 放回 templates
,因为 django 只会在这些文件夹中查找模板。
所以我正在尝试为我的 Django 项目的主页制作一个 index.html。我制作了一个名为页面的应用程序。
请注意模板文件夹是空的(移动 index.html 到模板没有任何作用)。 admin.py 和 models.py 也不包含任何内容。
我这样配置了模板设置:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join(BASE_DIR, 'templates'),
],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
在我的根项目文件夹中,urls.py 看起来像这样。
from django.contrib import admin
from django.urls import path
from django.conf.urls import include
# from pages.views import home_view
urlpatterns = [
path('homepage/', include('pages.urls')),
path('admin/', admin.site.urls),
path('explorer/', include('explorer.urls')),
]
在页面应用程序的 views.py 文件中:
from typing import ContextManager
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def index(request):
# Render the HTML template index.html
return render(request, 'index.html', context=ContextManager)
在页面应用程序(不是项目根文件夹)中,urls.py 看起来像这样(我自己创建了这个文件):
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
和index.html:
<!DOCTYPE html>
<title>Test</title>
<p>Test</p>
这是完整的文件结构。
这是我 运行 服务器时发生的情况:
其中 http://127.0.0.1:8000/ 是本地主机的根目录。
index.html 应该在模板文件夹中。 pages/templates/index.html
这是因为您的根 urls 没有 ''
:
urlpatterns = [
path('homepage/', include('pages.urls')),
path('admin/', admin.site.urls),
path('explorer/', include('explorer.urls')),
]
我猜你想 http://127.0.0.1:8000/
转到索引,这就是你写这个的原因:
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
但是使用此配置,索引将在 http://127.0.0.1:8000/homepage/
上。如果你想将其设置为 /
,请将根 url 配置更改为:
urlpatterns = [
path('admin/', admin.site.urls),
path('explorer/', include('explorer.urls')),
path('', include('pages.urls')),
]
最后,您需要将 html 放回 templates
,因为 django 只会在这些文件夹中查找模板。