我已经制作了两个文件
market.py crypto.html
market py中的代码从两个API地址提取数据,并且应该在我运行服务器并尝试打开crypto.html时在crypto.html中显示它们,并在/ crypto /
正如sof上的其他帖子所解释的,我尝试修改INSTALLED_APPS = [ 但是我无法使页面正常运行。
market.py
import requests, time
from django.shortcuts import render
class CryptoData:
def __init__(self):
self.usd_url = "https://api.coingecko.com/api/v3/coins/markets?" \
"vs_currency=usd&order=market_cap_desc&per_page=250&page=1"
\
"&sparkline=false&price_change_percentage=24h"
self.gbp_url = "https://api.coingecko.com/api/v3/coins/markets?" \
"vs_currency=gbp&order=market_cap_desc&per_page=250&page=1"
\
"&sparkline=false&price_change_percentage=24h"
self.previous_request = None
self.results1 = None
self.results2 = None
def get_crypto_data_dict(self, seconds_to_wait=60):
crypto_dict = dict()
if not self.previous_request or self.previous_request + seconds_to_wait <
time.time():
print("requested", self.previous_request, time.time())
self.previous_request = time.time()
crypto_dict = dict()
requests1 = requests.get(self.usd_url)
self.results1 = requests1.json()
requests2 = requests.get(self.gbp_url)
self.results2 = requests2.json()
for i in range(0, 250):
crypto_dict[self.results1[i]['id']] = {
'coin_name': self.results1[i]['name'],
'changes': self.results1[i]['price_change_percentage_24h'],
'usd': self.results1[i]['current_price'],
'gbp': self.results2[i]['current_price']
}
return crypto_dict
crypto_data = CryptoData()
def crypt_view(request):
context = {
'crypto_data': crypto_data.get_crypto_data_dict()
}
return render(request, 'crypto.html', context=context)
crypto.html
{% extends "blog/base.html" %}
{% block content %}
<ul>
{% for crypto_datum, crypto_value in crypto_data.items %}
<li>{{ crypto_datum }}
<ul>
{% for info, value in crypto_value.items %}
<li>{{ info }}: {{ value }}</li>
{% endfor %}
</ul>
</li>
{% endfor %}
</ul>
{% endblock content %}
views.py
from django.shortcuts import render
from django.http import HttpResponse
from django.views.generic import ListView, DetailView, CreateView
from coinGecko import market
from .models import Post
def home(request):
context = {
'posts': Post.objects.all()
}
return render(request, 'blog/home.html', context)
class PostListView(ListView):
model = Post
template_name = 'blog/home.html'
context_object_name = 'posts'
ordering = ['-date_posted']
class PostDetailView(DetailView):
model = Post
class PostCreateView(CreateView):
model = Post
fields = ['title', 'content']
def form_valid(self, form):
form.instance.author = self.request.user
return super().form_valid(form)
def about(request):
return render(request, 'blog/about.html', {'title': 'About'})
def crypt_view(request):
cryto_data = market.CryptoData
context = {
'crypto_data': market.crypto_data.get_crypto_data_dict()
}
return render(request, 'coinGecko/crypto.html', context=context)
我已经测试了market.py,当我设置它打印每行时它显示正确的数据。我能够在cmd输出中查看它。
我知道代码可以工作,但是我认为现在的问题是让market.py将数据输出到crypto.html
打开crypto.html时的当前输出:
TemplateDoesNotExist at /crypto/
coinGecko/crypto.html
Request Method: GET
Request URL: http://localhost:8000/crypto/
Django Version: 2.1.5
Exception Type: TemplateDoesNotExist
Exception Value:
coinGecko/crypto.html
Exception Location:
C:\Users\Ace\AppData\Local\Programs\Python\Python37\lib\site-
packages\django\template\loader.py in get_template, line 19
Python Executable:
C:\Users\Ace\Desktop\django_project\venv\Scripts\python.exe
Python Version: 3.7.2
Python Path:
['C:\\Users\\Ace\\Desktop\\django_project',
'C:\\Users\\Ace\\AppData\\Local\\Programs\\Python\\Python37\\python37.zip',
'C:\\Users\\Ace\\AppData\\Local\\Programs\\Python\\Python37\\DLLs',
'C:\\Users\\Ace\\AppData\\Local\\Programs\\Python\\Python37\\lib',
'C:\\Users\\Ace\\AppData\\Local\\Programs\\Python\\Python37',
'C:\\Users\\Ace\\Desktop\\django_project\\venv',
'C:\\Users\\Ace\\Desktop\\django_project\\venv\\lib\\site-packages',
'C:\\Users\\Ace\\AppData\\Roaming\\Python\\Python37\\site-packages',
'C:\\Users\\Ace\\AppData\\Local\\Programs\\Python\\Python37\\lib\\site-
packages']
Server time: Sun, 3 Feb 2019 20:35:00 +0000enter code here
跟踪:
Environment:
Request Method: GET
Request URL: http://localhost:8000/crypto/
Django Version: 2.1.5
Python Version: 3.7.2
Installed Applications:
['blog.apps.BlogConfig',
'users.apps.UsersConfig',
'crispy_forms',
'coinGecko',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Template loader postmortem
Django tried loading these templates, in this order:
Using engine django:
* django.template.loaders.filesystem.Loader:
C:\Users\Ace\Desktop\django_project\blog\
templates\blog\coinGecko\crypto.html
(Source does not exist)
* django.template.loaders.app_directories.Loader:
C:\Users\Ace\Desktop\django_project\blog\templates\coinGecko\crypto.html
(Source does not exist)
* django.template.loaders.app_directories.Loader:
C:\Users\Ace\Desktop\django_project\users\templates\coinGecko\crypto.html
(Source does not exist)
* django.template.loaders.app_directories.Loader:
C:\Users\Ace\AppData\Local\Programs\Python\Python37\lib\site-
packages\crispy_forms\templates\coinGecko\crypto.html (Source does not
exist)
* django.template.loaders.app_directories.Loader:
C:\Users\Ace\AppData\Local\Programs\Python\Python37\lib\site-
packages\django\contrib\admin\templates\coinGecko\crypto.html (Source
does not exist)
* django.template.loaders.app_directories.Loader:
C:\Users\Ace\AppData\Local\Programs\Python\Python37\lib\site-
packages\django\contrib\auth\templates\coinGecko\crypto.html (Source does
not exist)
File "C:\Users\Ace\AppData\Local\Programs\Python\Python37\lib\site-
packages\django\core\handlers\exception.py" in inner
34. response = get_response(request)
File "C:\Users\Ace\AppData\Local\Programs\Python\Python37\lib\site-
packages\django\core\handlers\base.py" in _get_response
126. response = self.process_exception_by_middleware(e,
request)
File "C:\Users\Ace\AppData\Local\Programs\Python\Python37\lib\site-
packages\django\core\handlers\base.py" in _get_response
124. response = wrapped_callback(request, *callback_args,
**callback_kwargs)
File "C:\Users\Ace\Desktop\django_project\blog\views.py" in crypt_view
46. return render(request, 'coinGecko/crypto.html', context=context)
File "C:\Users\Ace\AppData\Local\Programs\Python\Python37\lib\site-
packages\django\shortcuts.py" in render
36. content = loader.render_to_string(template_name, context,
request, using=using)
File "C:\Users\Ace\AppData\Local\Programs\Python\Python37\lib\site-
packages\django\template\loader.py" in render_to_string
61. template = get_template(template_name, using=using)
File "C:\Users\Ace\AppData\Local\Programs\Python\Python37\lib\site-
packages\django\template\loader.py" in get_template
19. raise TemplateDoesNotExist(template_name, chain=chain)
Exception Type: TemplateDoesNotExist at /crypto/
Exception Value: coinGecko/crypto.html
模板:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [r'C:\Users\Ace\Desktop\django_project\blog\templates\blog'],
'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',
],
},
},
]
通向crypto.html的文件结构
DjangoProject
/blog
/templates
/blog
/about.html
/base.html
/home.html
/coinGecko
/crypto.html
/market.py