Django模板错误“ TemplateDoesNotExist at”

时间:2019-01-09 13:04:38

标签: python django

我可以使用“ 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 %}

views.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)

url.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')
]

1 个答案:

答案 0 :(得分:1)

您需要对此进行修改

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