仍在学习 Django 的基础知识,我有点吃力。现在我正在尝试为我的站点创建一个主页。项目名称是“meal_planner”,应用程序是“meal_plans”。
meal_planner/urls.py 包含此代码:
from django.conf.urls import include, url
from django.contrib import admin
from django.urls import path
urlpatterns = [
# url(r'', include('meal_plans.urls', namespace='meal_plans'))
path('', include('meal_plans.urls', namespace='meal_plans'))
]
meal_plans/urls.py 包含这段代码:
"""Define URL patterns for meal_plans"""
from django.conf.urls import url
from . import views
app_name = 'meal_plans'
urlpatterns = [
# Home page
url(r'^$', views.index, name='index')
]
meal_plans/views.py 包含这段代码:
from django.shortcuts import render
# Create your views here.
def index(request):
"""Homepage"""
return render(request, 'main\index.html')
我得到的错误是
TemplateDoesNotExist at /
main\index.html
Template-loader postmortem
Django tried loading these templates, in this order:
Using engine django:
django.template.loaders.app_directories.Loader: ...\meal_planner\mp\lib\site-packages\django\contrib\admin\templates\main\index.html (Source does not exist)
django.template.loaders.app_directories.Loader: ...\meal_planner\mp\lib\site-packages\django\contrib\auth\templates\main\index.html (Source does not exist)
我已经尝试搜索如何执行此操作,但我就是做对了。感谢您的帮助。
最佳答案
在 meal_plans
中创建一个目录 templates
,然后将 index.html
添加到该目录。然后,如下更改代码,
def index(request):
"""Homepage"""
return render(request, 'index.html')
关于python - Django 2.0 使用 views.py 呈现 html 页面 (TemplateDoesNotExist),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50560521/