maufcost
maufcost

Reputation: 169

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.

enter image description here

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).

Upvotes: 0

Views: 139

Answers (2)

ahmet
ahmet

Reputation: 347

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

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

Upvotes: 0

Alasdair
Alasdair

Reputation: 308769

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.

Upvotes: 1

Related Questions