首页 > 解决方案 > 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')
]

标签: pythondjango

解决方案


你需要修改这个

def CustomerList(request):
    ...
    return render(request, 'customer/customer_list.html', data)

推荐阅读