Django: 未找到模板/index.htm

2024-04-18 12:53:23 发布

您现在位置:Python中文网/ 问答频道 /正文

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

你知道吗视图.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',
        },
    },
},
]

你知道吗网址.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) 当前路径,索引.html,与这些都不匹配。你知道吗


Tags: pathdjangonameimportappindexosviews
2条回答

你必须渲染模板,并修复缩进

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

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

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

更改:

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 defines a list of directories where the engine should look for template source files, in search order.

相关问题 更多 >