2017-08-25 67 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'。