python - Django 2.0 使用 views.py 呈现 html 页面 (TemplateDoesNotExist)

标签 python django

仍在学习 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/

相关文章:

python - AttributeError at/accounts/login/'dict' 对象没有属性 'status_code'

python - 在 Django 上进行测试时遇到的问题

python - `strip` ing 在 python 中拆分的结果

python - 如何让 POS n-gram 更有效?

python - 从字符串日期中提取一年中的某一天和儒略日

python - 如何将 Pandas 列多索引名称作为列表获取

django - kombu utils 对象 py line 42 in __ get __ return obj __ dict __[ self __ name __] keyerror data

django - 更改 DRF 路由器中的查找正则表达式

python - Django "Instance"表单字段未渲染

python - 从 groupby.size() 命令查找总计