簡體   English   中英

Django找不到我的模板

[英]Django cannot find my templates

我已經嘗試了許多方法來“使” Django找到我的模板,但是我無法弄清楚出了什么問題。 Django如何找到模板? 它總是引發TemplateDoesNotExist異常。

它設法獲取位於“ templates”文件夾內的“ base.html”模板,但不能設法獲取“ post”文件夾內的“ list.html”和“ detail.html”模板,也位於“模板”文件夾中。

在此處輸入圖片說明

這些是視圖:

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});

如果將“ templates / post / list.html”放在render函數的第二個參數中,為什么它不起作用? (這也是我嘗試使事情起作用的另一種方式)。

您的list.htmlpost.html模板位於post目錄中,因此在指定模板路徑時必須將其包括在內。 例如:

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

Django會自動在templates目錄中搜索已安裝的應用程序中的每個應用程序,包括blog/templates ,因此在指定模板路徑時不應包括templates

您還可以修改基本settings.py文件以重定向模板部分。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM