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

时间:2018-05-28 06:43:00

标签: 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)

我已经尝试过如何做到这一点,但我无法做到这一点。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:1)

templates内创建一个目录meal_plans,然后将index.html添加到该目录。然后,按以下方式更改您的代码,

def index(request):
    """Homepage"""
    return render(request, 'index.html')