這篇文章給大家介紹django中怎么使用Highcharts.js實(shí)現(xiàn)可視化數(shù)據(jù),內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。
創(chuàng)新互聯(lián)服務(wù)項(xiàng)目包括遂寧網(wǎng)站建設(shè)、遂寧網(wǎng)站制作、遂寧網(wǎng)頁(yè)制作以及遂寧網(wǎng)絡(luò)營(yíng)銷策劃等。多年來(lái),我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢(shì)、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,遂寧網(wǎng)站推廣取得了明顯的社會(huì)效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到遂寧省份的部分城市,未來(lái)相信會(huì)繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!
新建project和app
django-admin startproject highcharts
cd highcharts
python manage.py startapp clusterbar
在clusterbar/models.py文件里添加代碼
class PopulationByRegion(models.Model):
region = models.CharField(max_length=50)
pp1800 = models.IntegerField()
pp1900 = models.IntegerField()
pp2012 = models.IntegerField()
highcharts/urls.py文件代碼
from django.contrib import admin
from django.urls import path
from django.urls import include
urlpatterns = [
path('admin/', admin.site.urls),
path('clusterbar/',include('clusterbar.urls')),
]
在clusterbar文件夾下新建urls.py文件,寫入代碼
from django.urls import path
from . import views
urlpatterns = [
path('popbyregion/',views.popbyregion,name="popbyregion"),
]
在clusterbar的views.py文件中寫入代碼
from django.shortcuts import render
from .models import PopulationByRegion
# Create your views here.
def popbyregion(request):
df = PopulationByRegion.objects.all()
region = []
pp1800 = []
pp1900 = []
pp2012 = []
for info in df:
region.append(info.region)
pp1800.append(info.pp1800)
pp1900.append(info.pp1900)
pp2012.append(info.pp2012)
context = {'region':region,'pp1800':pp1800,'pp1900':pp1900,'pp2012':pp2012}
return render(request,'popbyregion.html',context=context)
在highcharts文件夾下新建templates文件夾,在templates文件夾下新建popbyregion.html文件 并寫入代碼
Django Highcharts Example
注冊(cè)app
添加模板路徑
python manage.py makemigrations
python manage migrate
python manage.py shell
import csv
from clusterbar.models import PopulationByRegion
with open('example.csv') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
p = PopulationByRegion(region=rwo['continent'],
pp1800=int(row["year_1800"]),
pp1900=int(row['year_1900']),
pp2012=int(row['year_2012']))
p.save()
quit()
python manage.py createsuperuser
依次輸入用戶名、郵箱密碼 注冊(cè)數(shù)據(jù) 在clusterbar文件夾下的admin.py中添加代碼
from django.contrib import admin
from .models import PopulationByRegion
# Register your models here.
admin.site.register(PopulationByRegion)
啟動(dòng)服務(wù)器可以看到數(shù)據(jù)已經(jīng)添加過來(lái)了
https://github.com/sea-team/simpleui#%E5%BC%80%E5%A7%8B%E4%BD%BF%E7%94%A8
按照以上鏈接進(jìn)行配置 后臺(tái)變成了這樣
輸入鏈接http://127.0.0.1:8000/clusterbar/popbyregion/
可以看到結(jié)果
過程中我遇到了一個(gè)報(bào)錯(cuò)
django.core.exceptions.ImproperlyConfigured: The included URLconf '
關(guān)于django中怎么使用Highcharts.js實(shí)現(xiàn)可視化數(shù)據(jù)就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。