我正在尝试在plots.py上显示由plotly创建的散点图,并在Simulator.html上显示html。 问题是,当我使用以下代码运行服务器时,它显示如下访问http://127.0.0.1:8000/simulator/时的基本错误。 如您在settings.py上看到的,我相信所有必要的设置都应该已经完成。 你们能找出导致此错误的原因以及如何解决该错误吗?
错误消息:
Maven/Gradle
simulator.html
TemplateDoesNotExist at /simulator/
simulator.html
Request Method: GET
Request URL: http://127.0.0.1:8000/simulator/
Django Version: 2.2.4
Exception Type: TemplateDoesNotExist
Exception Value:
simulator.html
Exception Location: /Users/user/.pyenv/versions/3.7.3/lib/python3.7/site-packages/django/template/loader.py in select_template, line 47
Python Executable: /Users/user/.pyenv/versions/3.7.3/bin/python3
Python Version: 3.7.3
Python Path:
['/Users/user/Desktop/Django/price_simulator',
'/Users/user/.pyenv/versions/3.7.3/lib/python37.zip',
'/Users/user/.pyenv/versions/3.7.3/lib/python3.7',
'/Users/user/.pyenv/versions/3.7.3/lib/python3.7/lib-dynload',
'/Users/user/.pyenv/versions/3.7.3/lib/python3.7/site-packages',
'/Users/user/.pyenv/versions/3.7.3/lib/python3.7/site-packages/IPython/extensions']
Server time: Tue, 24 Sep 2019 11:14:59 +0000
提取settings.py:
{% extends 'base.html' %}
{% block customcss %}
{% endblock customcss %}
{% block header %}
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
</div>
{% endblock header %}
<div class="card" style="width: 100%;margin-top: 15px">
<div class="card-body">
<h5 class="card-title">title</h5>
{{ scatter|safe }}
</div>
</div>
应用文件夹中的url.py:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'simulatorapp'
]
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [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',
],
},
},
]
views.py:
from django.shortcuts import render
from django.http.response import HttpResponse
from django.contrib.auth.models import User
from django.contrib.auth import authenticate, login
from django.shortcuts import redirect
import pandas as pd
from django.views.generic import TemplateView
from . import plots
class ScatterView(TemplateView):
template_name='simulator.html'
def get_context_data(self, **kwargs):
context = super(ScatterView, self).get_context_data(**kwargs)
context['scatter'] = plots.get_scatter()
return context
答案 0 :(得分:0)
这与URL或视图无关。如错误所示,Django无法找到您的模板;这是因为您没有正确指定DIRS。应该是:
'DIRS': [os.path.join(BASE_DIR, 'templates')],