简体   繁体   中英

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. How does Django find templates? It is always raising the TemplateDoesNotExist exception.

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.

在此处输入图片说明

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? (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. 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.

you can also modify your base settings.py file to redirect the templates part.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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