【问题标题】:TemplateDoesNotExist at / index.htmlTemplateDoesNotExist at / index.html
【发布时间】:2015-03-01 09:56:04
【问题描述】:

我的 Django 项目有一些问题。

我是使用 django 制作 Web 应用程序的新手。请帮帮我。

所以,

这是我的目录路径,

-lullabyluna
----app_index
------------admin.py
------------index.html
------------__init__.py
------------migtations
------------models.py
------------tests.py
------------views.py
----index.html(I wonder this files path.)
----lullabyluna
------------__init__.py
------------settings.py
------------url.py
------------wsgi.py
----manage.py
----static

我的 settings.py 文件包括:

INSTALLED_APPS = (


    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'app_index',
)

这是我的 url.py 文件

from django.conf.urls import patterns, include, url
from django.contrib import admin

urlpatterns = patterns('app_index.views',
    # Examples:
    url(r'^$', 'home', name='home'),
    # url(r'^blog/', include('blog.urls')),
    url(r'^admin/', include(admin.site.urls)),
)

最后这是我的 app_index/views.py

from django.template import RequestContext
from django.shortcuts import render_to_response

def home(request):
    return render_to_response('index.html',{},context_instance=RequestContext(request))

所以,我想知道正确的“index.html”文件路径在哪里。

【问题讨论】:

  • TEMPLATE_DIRS设置的值是多少?

标签: python django python-2.7


【解决方案1】:

在 settings.py 中添加这个

TEMPLATE_DIRS = (
    PROJECT_PATH 
)

(或) 将您的 index.html 移动到模板文件夹并执行此操作,

TEMPLATE_DIRS = (
    PROJECT_PATH + '/templates/',
)

【讨论】:

  • 哇,第一个问题解决了,但是我有“502 Bad gateway”..这是路径问题吗?
  • 好吧,我认为我的 Django 出现了一些错误(错误页面向我显示了 nginx 页面)。我的 Django 版本是 1.7.1,所以 Templeate_dirs 可以调整吗?
  • 如果是 502 错误网关,则问题不在 django 内部。您可能没有正确配置您的 nginx + uwsgi(可能)。 Nginx 本身无法处理 py 脚本。它需要一个解释器——就像在 wsgi 服务器中一样。
  • 但在我制作 app_index 应用程序之前,它运行良好。 django欢迎页面出现,当我用app_index修改相关文件时,它就崩溃了。 “Template_dirs”可以应用在 Django 1.7.1 上吗?
  • 好的,我明白了! TEMPLATE_DIRS = ( os.path.join(BASE_DIR), ) 这就是答案!非常感谢!
【解决方案2】:

在 pythonwhere.com 中,您需要在 settings.py 中执行此操作

'DIRS': ['/home/your_user_name/your_project/templates'],

【讨论】:

    【解决方案3】:

    在主项目文件夹的“settings.py”中提及您的应用名称:

    INSTALLED_APPS = [
        'Your_AppName',
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
    ]
    

    【讨论】:

      【解决方案4】:

      我认为,问题可能出在“app_index/views.py”。 你要做的是,在 app 文件夹(app_index)中创建一个模板文件夹。 在模板文件夹中,创建另一个名为(app_index)的文件夹,然后将 index.html 文件放在这里。 在你这样做之后,在你的 app_index/views.py 中, 改变这个; " return render_to_response('index.html',{},context_instance=RequestContext(request)) "

            to;
      
      return render_to_response('app_index/index.html',{},context_instance=RequestContext(request))
      

      希望它对你有用,如果所有其他东西都没有问题的话。

      【讨论】:

        【解决方案5】:
        from django.template import RequestContext
        from django.shortcuts import render_to_response
        
        def home(request):
            return render_to_response('index.html',{},context_instance=RequestContext(request))
        

        您缺少通往应用程序索引的路径。而不是上面的,应该是

        def home(request):
            return render_to_response('**app_name/**index.html',{},context_instance=RequestContext(request))
        

        【讨论】:

          【解决方案6】:

          我已经尝试了很多使用 render_to_response 来做到这一点,但请注意 django 仍然不使用 render_to_response。 使用 render 而不是 render_to_response

          所以,

          在文件夹 {The_name_of_the_Project} 中,去编辑 settings.py

          TEMPLATES 中,用包含静态 html 文件的文件夹的路径填写 DIRS。 如果 .html 文件存在于您的 {The_name_of_the_Project} 文件夹中,则路径类似于:'/home/your_username/The_name_of_the_Project/'。

          如果静态 .html 文件包含在 {The_name_of_the_Project} 文件夹中,那么 DIRS 必须如下所示:DIRS=['/home/your_username/your_project_name/']

          【讨论】:

            【解决方案7】:

            在此处输入代码 lullabyluna ....将您的模板(index.html、home.html 等)放在这里,并将您的静态文件夹放在这里 现在你在索引 {% load static%} 之后得到你的索引和静态文件 和 html {% block content %} 和 {% endblock %} 的结尾 现在你的页面是动态的

            【讨论】:

            • 很难理解你的意思。也许使用一些代码格式化会有所帮助。
            猜你喜欢
            • 2016-08-27
            • 1970-01-01
            • 2016-12-07
            • 1970-01-01
            • 2020-06-20
            • 2018-09-29
            • 2019-06-01
            • 1970-01-01
            • 2022-01-11
            相关资源
            最近更新 更多