簡體   English   中英

/ hello / Django錯誤中的TemplateDoesNotExist

[英]TemplateDoesNotExist at /hello/ Django error

我是django python的初學者。 盡管我的模板有正確的目錄,但是卻出現上述錯誤。

我的view.py就是這樣。

from __future__ import unicode_literals
from django.http import HttpResponse
from django.shortcuts import render
def hello(request):
    return render(request,"C:/Users/Vivek/myproject/myapp/template/hello.html",{})

我在C:\\ Users \\ Vivek \\ myproject \\ myapp \\ template中有hello.html

以下是我遇到的錯誤。

TemplateDoesNotExist at /hello/
C:/Users/Vivek/myproject/myapp/template/hello.html
Request Method: GET
Request URL:    http://127.0.0.1:8000/hello/
Django Version: 1.11.10
Exception Type: TemplateDoesNotExist
Exception Value:    
C:/Users/Vivek/myproject/myapp/template/hello.html

鏈接模板是一種不好的方法。 而是檢查您的設置文件並定義OS絕對路徑。 它看起來像這樣:

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

從更改您的代碼

return render(request,"C:/Users/Vivek/myproject/myapp/template/hello.html",{})

對此

return render(request,"hello.html",{})

它應該工作。

Django的方法是在設置文件中定義模板的目錄

'DIRS': [os.path.join(BASE_DIR, 'templates')],

因此,只需將此行添加到您的TEMPLATES設置中,就可以了。

Django會自動在您傳遞的目錄中搜索模板,您只需要在render方法中指定模板名稱即可。

暫無
暫無

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

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