python - Django 模板错误“TemplateDoesNotExist at”
问题描述
我可以使用“py manage.py runserver”运行我的服务器而不会出现任何错误,然后我的登录页面出现并登录到系统,当我单击该页面中的 customer_list 链接时,主欢迎页面会显示给我一个错误,如下所示.
错误
TemplateDoesNotExist at /clist
customer_list
Request Method: GET
Request URL: http://127.0.0.1:8000/clist
Django Version: 2.0.2
Exception Type: TemplateDoesNotExist
Exception Value:
customer_list
Template-loader postmortem
Django tried loading these templates, in this order:
Using engine django:
django.template.loaders.filesystem.Loader: D:\project\dysapp\templates\customer_list (Source does not exist)
django.template.loaders.app_directories.Loader: D:\project\dysapp\templates\customer_list (Source does not exist)
django.template.loaders.app_directories.Loader: C:\Users\cenk.adalan\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\contrib\admin\templates\customer_list (Source does not exist)
django.template.loaders.app_directories.Loader: C:\Users\cenk.adalan\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\contrib\auth\templates\customer_list (Source does not exist)
django.template.loaders.app_directories.Loader: C:\Users\cenk.adalan\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django_tables2\templates\customer_list (Source does not exist)
django.template.loaders.app_directories.Loader: C:\Users\cenk.adalan\AppData\Local\Programs\Python\Python37-32\lib\site-packages\import_export\templates\customer_list (Source does not exist)
Settings.py - 模板配置
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, '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',
],
},
},]
登录到主页后,我单击“customer_list”处的此链接并出现错误
{% if user.is_authenticated %}
<p></p>
{% if perms.dysapp.add_bloodtype %}
<li class="passive"><a href="{% url "customer_list" %}"> <i class="fa fa-link"></i> <span>test2</span></a></li>
<li class="passive"><a href="{% url "login" %}"> <i class="fa fa-link"></i> <span>test</span></a></li>
customer_link.html
{% extends 'ltebase.html' %}
{% block title %}Danışanlar{% endblock %}
{% block content %}
{% for customer in object_list %}
<li>{{ customer.name }}</li>
{% empty %}
<li>No customer exists.</li>
{% endfor %}
{% endblock %}
视图.py
from django.shortcuts import render, redirect
from django.contrib.auth.forms import UserCreationForm, AuthenticationForm
from django.contrib.auth.decorators import login_required
from django.http import HttpResponse
from .models import Customer
from django.urls import reverse_lazy
def CustomerList(request, template_name='customer/customer_list.html'):
customer = Customer.objects.all()
data = {}
data['object_list'] = customer
return render(request, template_name, data)
网址.py
from django.contrib import admin
from django.urls import path
from django.conf.urls import include, url
from django.conf import settings
from django.views.generic.base import TemplateView
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.conf.urls.static import static
from django.contrib.auth import views as auth_views
from dysapp.views import main_view, ltebase_view, CustomerList
admin.autodiscover()
urlpatterns = [
path('sapbca_/', admin.site.urls),
# url(r'^admin/', admin.site.urls),
path('', include('django.contrib.auth.urls'), name='login'),
url(r'^$', auth_views.login, name='login'),
url(r'^main/', main_view, name='main'),
url(r'^ltebase/', ltebase_view, name='ltebase'),
path('clist', CustomerList, name='customer_list')
]
解决方案
你需要修改这个
def CustomerList(request):
...
return render(request, 'customer/customer_list.html', data)
推荐阅读
- python - Roslaunch 在启动节点时得到“没有这样的文件或目录”
- c++ - 声明函数指针和初始化 C++
- ipad - 此网页出现问题,因此已重新加载
- c - 如何使用 json-c 转换几个 JSON 对象?
- reactjs - 安全性:我可以在本地更改(例如在 chrome 中).env 变量、react 状态或 redux 状态吗?
- ios - Unity 2019.3.7f1 ios 未构建
- c++ - 为什么 C++20 不支持“void f(Concept const auto&)”?
- java - 在 Spring Boot 中使用 hibernate-types-52 时如何禁用 Hypersistence 横幅?
- python-3.x - python(熊猫)中的嵌套Excel数据透视表
- r - R:错误:自动绘图不支持数字类型的对象