简体   繁体   English

Django找不到我的模板

[英]Django cannot find my templates

I have already tried many ways to "make" Django find my templates, but I cannot figure out what is wrong. 我已经尝试了许多方法来“使” Django找到我的模板,但是我无法弄清楚出了什么问题。 How does Django find templates? Django如何找到模板? It is always raising the TemplateDoesNotExist exception. 它总是引发TemplateDoesNotExist异常。

It is managing to get the "base.html" template which is inside the "templates" folder, but it is not managing to get the "list.html" and "detail.html" templates that are inside a "post" folder, which is also inside the "templates" folder. 它设法获取位于“ templates”文件夹内的“ base.html”模板,但不能设法获取“ post”文件夹内的“ list.html”和“ detail.html”模板,也位于“模板”文件夹中。

在此处输入图片说明

These are the views: 这些是视图:

def post_list(request):

    posts = Post.published.all(); # '.published' is a manager.

    # try:
    return render(request, "list.html", {"posts": posts});

    # except:
        # return render(request, "base.html", {"posts": posts});

def post_detail(request, year, month, day, post):
    post = get_object_or_404(Post, slug=post,
                                    status="published",
                                    publish_year=year,
                                    publish_month=month,
                                    publish_day=day);

    return render(request, "detail.html", {"post": post});

Why doesn't it work if I put "templates/post/list.html" in the second argument of the render function? 如果将“ templates / post / list.html”放在render函数的第二个参数中,为什么它不起作用? (That's also another way that I tried to make things work). (这也是我尝试使事情起作用的另一种方式)。

Your list.html and post.html templates are in a post directory, so you have to include that when specifying the template path. 您的list.htmlpost.html模板位于post目录中,因此在指定模板路径时必须将其包括在内。 For example: 例如:

return render(request, "post/list.html", {"posts": posts});

Django automatically searches the templates directory for every app in your installed apps, including blog/templates , so you shouldn't include templates when you specify the template path. Django会自动在templates目录中搜索已安装的应用程序中的每个应用程序,包括blog/templates ,因此在指定模板路径时不应包括templates

you can also modify your base settings.py file to redirect the templates part. 您还可以修改基本settings.py文件以重定向模板部分。

TEMPLATES = [
        'DIRS': [os.path.join(BASE_DIR, '/templates'],

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM