from django.views import View
import plotly
from django.shortcuts import render
from plotly.offline import plot
from plotly.graph_objs import Scatter
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from . import models
class GoodDetail(View):
def get(self, request, detail_id):
data_list = list(
models.DWGoodInfo.objects.filter(detail_id=detail_id).order_by('-update_time').values())
x_data = []
y_data = []
for da in data_list:
update_time = str(da.get("update_time", ""))
x_data.append(update_time)
y_data.append(da.get("price", 0))
layout = plotly.graph_objs.Layout(xaxis={'type': 'category'})
data = Scatter(x=x_data, y=y_data, mode='lines', name='test', opacity=0.8, marker_color='red')
fig = plotly.graph_objs.Figure([data], layout)
plot_div = plot(fig, output_type='div')
context = {'plot_div': plot_div}
return render(request, 'detail.html', context=context)
{{ plot_div | safe }}
from django.urls import path
urlpatterns = [
path('detail/<detail_id>', GoodDetail.as_view(),name='detail'),
]
参考: https://www.coder.work/article/4940159 https://debug.casa/read-10623781.html https://cloud.tencent.com/developer/article/1632737
|