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

2024-03-29 08:55:09 发布

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

{% 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

这是url.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'),

]

这是my 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 运行代码时会显示这种类型的错误。我已经包含了模板,但显示了错误。这可能是模板结构错误。如果有人能指导我在哪里出错,这将很有帮助


Tags: djangofrompyimportdivurldataadmin
2条回答

试试这个

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

您的base.html位于chartapp目录中,因此extends标记应为:

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

相关问题 更多 >