【问题标题】:Django : Template not found/index.htmlDjango:找不到模板/index.html
【发布时间】:2019-10-22 19:29:00
【问题描述】:

我正在使用 Django 运行一个网站。登录没有问题。当我登录并单击某个仪表板时,它显示“找不到页面”(404)。

Views.py:

def index(request):
return(request,'obs_app/index.html')

设置.py

import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_DIR = os.path.join(BASE_DIR,'templates/index.html')
TEMPLATES = [
{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [TEMPLATE_DIR,],
    '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',
            ],
        'libraries':  {
            'get_by_index':'obs_app.templatetags.templatefilters',
            'get_by_key':'obs_app.templatetags.templatefilters',
            'get_dict':'obs_app.templatetags.templatefilters',
            'get_items':'obs_app.templatetags.templatefilters',
            'multiple':'obs_app.templatetags.templatefilters',
        },
    },
},
]

urls.py:

from django.contrib import admin
from django.urls import path
from django.conf.urls import url
from obs_app import views
urlpatterns = [
url(r'index/',views.index , name = 'index'),
path('admin/', admin.site.urls),
path('', views.user_login, name='user_login'),
path('dashboard',views.obs_index, name='admin_dash'),
path('halls/active',views.obs_halls_active,name='active-halls'),
path('halls/pending',views.obs_halls_pending,name='pending-halls'),
path('febs/userlist', views.febs_user_list, name='febs-users'),
path('bookings/user', views.bookings_user, name='bookings-users'),
path('bookings/owner', views.bookings_owner, name='bookings-owner'),
path('cancellation/user', views.cancelled_user, name='cancelled-user'),
path('cancellation/owner', views.cancelled_owner, name='cancelled-owner'),
path('terms/obs', views.terms_conditions, name='terms-conditions'),
path('terms/febs', views.terms_febs, name='terms-febs'),
path('terms/febs/events', views.terms_febs_events, name='terms-febs-events'),

我得到的错误是: 找不到页面 (404) 当前路径 index.html 与其中任何一个都不匹配。

【问题讨论】:

  • 更改为return render(request,'index.html')
  • 您的TEMPLATE_DIR 应该是一个目录,而不是一个模板文件。
  • 404 是“找不到模板”的单独问题。模板名称和 URL 在 Django 中是分开的。您的浏览器中有url(r'index/, ...), so you would go to /index/`,而不是index.html。

标签: python django


【解决方案1】:

您必须渲染模板并修复缩进

def index(request):
   return render(request,'obs_app/index.html')

而且这里你需要提供目录而不是文件

TEMPLATE_DIR = os.path.join(BASE_DIR, 'templates')

【讨论】:

    【解决方案2】:

    变化:

    TEMPLATE_DIR = os.path.join(BASE_DIR,'templates/index.html')
    

    收件人:

    TEMPLATE_DIR = os.path.join(BASE_DIR, 'templates')
    

    TEMPLATES = [
        {
            ...
            'DIRS': [],    # DIRS defines a list of directories where the engine should look for template source files, in search order.
            ...
        },
    ]
    

    您可能想阅读:Support for template engines

    相关部分复制到这里:

    DIRS 定义了引擎应按搜索顺序查找模板源文件的目录列表。

    【讨论】:

      猜你喜欢
      • 2015-03-12
      • 2018-01-02
      • 2012-07-10
      • 2015-03-14
      • 2020-05-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多