模板不存在错误...在Django中运行manage.py时出错

时间:2018-10-11 12:11:13

标签: python django

{% extends 'chartapp/base.html' %}

<script>
    {% block jquery %}
    {% endblock %}
</script>

{% block content %}
<div class='row'>
    <div clas = 'col-sm-12'>
        <h1>Hello world!!</h1>
    </div>
</div>
{% endblock content %}

这是chart.html的简单代码,目录的结构是

(venv) sevenbits@sevenbits-H110M-H:~/chart-django/chart$ tree
.
├── chart
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── settings.py
│   ├── settings.pyc
│   ├── urls.py
│   ├── urls.pyc
│   ├── views.py
│   ├── views.pyc
│   ├── wsgi.py
│   └── wsgi.pyc
├── chartapp
│   ├── admin.py
│   ├── admin.pyc
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── migrations
│   │   ├── __init__.py
│   │   └── __init__.pyc
│   ├── models.py
│   ├── models.pyc
│   ├── templates
│   │   └── chartapp
│   │       ├── base
│   │       │   ├── bootstrap_defaults.html
│   │       │   ├── css.html
│   │       │   └── js.html
│   │       ├── base.html
│   │       └── charts.html
│   ├── tests.py
│   ├── views.py
│   └── views.pyc
├── db.sqlite3
└── manage.py

这是urls.py文件

from django.conf.urls import patterns, include, url
from django.contrib import admin
from .views import HomeView, get_data

urlpatterns = [

    url(r'^admin/', include(admin.site.urls)),
    url(r'^$',HomeView.as_view(), name='home'),
    url(r'^api/data/$', get_data, name='api-data'),

]

这是我的views.py文件

from django.http import JsonResponse
from django.shortcuts import render
from django.views.generic import View

class HomeView(View):
        def get(self, request, *args, **kwargs):
                return render(request, 'chartapp/charts.html', {})

def get_data(request, *args, **kwargs):
        data = {
                "sales": 100,
                "customers": 10,
        }
        return JsonResponse(data)
在Charts.html上的

TemplateDoesNotExist 运行代码时会显示这种类型的错误。我已经包含了模板,但显示了错误。这可能是模板结构错误。如果有人可以指导我在哪里犯错,将会很有帮助。

2 个答案:

答案 0 :(得分:2)

您的base.htmlchartapp目录中,因此您的extends标签应为:

{% extends 'chartapp/base.html' %}

答案 1 :(得分:2)

尝试

class HomeView(View):
    def get(self, request, *args, **kwargs):
            return render(request, 'chartapp/charts.html', {})