2017-08-25 70 views
0

我有一個django應用程序和主頁的視圖,我想在主頁上使用靜態html文件。我的文件結構是這樣的:Django:將靜態html文件傳遞到視圖中會產生TemplateDoesNotExist錯誤

project/ 
    stats (this is my app)/ 
    urls.py 
    views.py 
    stats.html 
    (other files here) 

在我的views.py我有以下代碼:

from django.shortcuts import render_to_response 

def index(request): 
    return render_to_response('stats.html') 

當我運行的網址,我只是索引頁。我進入統計頁面,收到TemplateDoesNotExist錯誤。尋找答案我試圖把stats/stats.html而不是路徑,但我仍然得到相同的錯誤。這樣做的正確方法是什麼?

+0

請發佈完整的錯誤 – csling

+0

@csling這是服務器對我發出的錯誤:https://pastebin.com/5CwXvAZA – PolarBearITS

回答

0

在您的項目目錄中,添加一個名爲'templates'的目錄,並在templates目錄中創建'stats'目錄。把你的HTML文件放到統計目錄中。

然後從底部

TEMPLATE_DIRS = (

    os.path.join(SETTINGS_PATH, 'templates'), 

) 

TEMPLATE_DIRS = (

    os.path.join(BASE_PATH, 'templates'), 

) 
+0

如何讓django識別模板目錄?我把路徑放到我的settings.py中作爲TEMPLATE_DIRS,但是django說它使用了'C:\ Python36-32 \ lib \ site packages \ django \ contrib \ admin \ templates \ stats.html'的默認模板路徑。我該如何重定向搜索? – PolarBearITS

+0

分享設置文件。也看看這個。 https://stackoverflow.com/questions/3038459/django-template-path – csling

+0

這裏的設置文件:https://pastebin.com/zwBDuDrc – PolarBearITS

0

你可以安排你的項目像這樣

project/ 
    stats (this is my app)/ 
     urls.py 
     views.py 
     stats.html 
     templates/ 
      stats/ 
       stats.html 

在你views.py改變你的設置文件,寫這:

def index(request): 
    return render_to_response('stats/stats.html') 

請確保settings.py中的INSTALLED_APPS列表中有'stats'。