【问题标题】:TemplateDoesNotExist Error in PythonanywherePythonanywhere 中的 TemplateDoesNotExist 错误
【发布时间】:2018-12-11 04:08:01
【问题描述】:

Erorr

我想知道 django 在哪里搜索 index.html

设置.py

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': ['templates'],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

views.py

def index(request):
     return render(request, 'index.html', {})

【问题讨论】:

  • 我觉得这个问题太笼统了,不清楚。您的 'DIRS': ['templates'], 不正确,请查看此 SO 问题中的所有 answers 以正确配置。请注意,默认情况下,Django 会在DIRS': ['/path/'] 中指定的路径中检索您的所有应用模板
  • 我有模板文件夹和 index.html 文件...它在本地工作而不是在 pythonanywhere....
  • 您需要在DIRS 中指定绝对路径,将您的path 加入Django 的BASE_DIR,如下所示:'DIRS': [os.path.join(BASE_DIR, 'templates'),],
  • 它工作......但我无法验证你的答案

标签: python django django-templates django-views pythonanywhere


【解决方案1】:

Django 将在模板目录中查找,即如果您创建了一个名为模板的文件夹,并且模板文件夹中有一个名为 index.html 的文件。出于某种原因,我觉得您问这个问题的原因是因为您在运行服务器时无法让您的模板呈现,对吗?请回复我,我会尽我所能为您填补空缺,让您的项目顺利进行。

【讨论】:

  • 我有模板文件夹和 index.html 文件...它在本地工作而不是在 pythonanywhere....
【解决方案2】:

你可以轻松解决这个问题:如果你的项目目录,manage.py所在的目录名称是“RalphPortfolio”,你需要在settings.py中进行如下修正:

'DIRS': ['RalphPortfolio/templates'],  

【讨论】:

    【解决方案3】:

    添加模板目录的完整路径 例如,如果您的路径是 /home/some_url/your_project/templates

    TEMPLATES = [
        {
            'BACKEND': 'django.template.backends.django.DjangoTemplates',
            'DIRS': ['/home/some_url/your_project_name/templates'],
            'APP_DIRS': True,
            'OPTIONS': {
                'context_processors': [
                    'django.template.context_processors.debug',
                    'django.template.context_processors.request',
                    'django.contrib.auth.context_processors.auth',
                    'django.contrib.messages.context_processors.messages',
                ],
            },
        },
    ]
    

    是因为路径不清楚,所以找不到。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-03
      • 2014-02-19
      • 2013-06-20
      • 2015-09-02
      • 2018-09-10
      • 2023-03-07
      • 2012-12-18
      • 1970-01-01
      相关资源
      最近更新 更多