第一步:找到pyecharts模块文件目录-render-templates目录
把templates目录下所有html文件负责到 django templates目录下
setting.py
""" Django settings for django_pyecharts project. Generated by 'django-admin startproject' using Django 3.2.9. For more information on this file, see https://docs.djangoproject.com/en/3.2/topics/settings/ For the full list of settings and their values, see https://docs.djangoproject.com/en/3.2/ref/settings/ """ """ django 框架总配置文件 数据库 mysql redis mongodb 中间件 缓存 集群 网站默认语言 """ import os.path from pathlib import Path # Build paths inside the project like this: BASE_DIR / 'subdir'. BASE_DIR = Path(__file__).resolve().parent.parent # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! SECRET_KEY = 'django-insecure-8ne-epnt666g!u(695szcxmvi8hl3@d&sux@qn45e#3r8j2ecz' # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True ##运行所有ip访问 ALLOWED_HOSTS = ['*'] # Application definition ####网站子应用注册 INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'test_pyecharts' ###子应用文件夹 ] 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', ] ROOT_URLCONF = 'django_pyecharts.urls' TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [os.path.join(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', ], }, }, ] WSGI_APPLICATION = 'django_pyecharts.wsgi.application' # Database # https://docs.djangoproject.com/en/3.2/ref/settings/#databases DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': BASE_DIR / 'db.sqlite3', } } # Password validation # https://docs.djangoproject.com/en/3.2/ref/settings/#auth-password-validators AUTH_PASSWORD_VALIDATORS = [ { 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', }, { 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', }, { 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', }, { 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', }, ] # Internationalization # https://docs.djangoproject.com/en/3.2/topics/i18n/ LANGUAGE_CODE = 'zh-hans' TIME_ZONE = 'Asia/Shanghai' USE_I18N = True USE_L10N = True USE_TZ = False # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/3.2/howto/static-files/ STATIC_URL = '/static/' # Default primary key field type # https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
url.py
"""django_pyecharts URL Configuration The `urlpatterns` list routes URLs to views. For more information please see: https://docs.djangoproject.com/en/3.2/topics/http/urls/ Examples: Function views 1. Add an import: from my_app import views 2. Add a URL to urlpatterns: path('', views.home, name='home') Class-based views 1. Add an import: from other_app.views import Home 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') Including another URLconf 1. Import the include() function: from django.urls import include, path 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) """ from django.contrib import admin from django.urls import path from test_pyecharts.views import hello,pyechart_demo ####导入test**.views文件中hello函数 urlpatterns = [ path('admin/', admin.site.urls), path('',hello), path('pyechart_demo/',pyechart_demo), ]
views.py
from django.shortcuts import render # Create your views here. from jinja2 import Environment,FileSystemLoader ####jinja2 专门去渲染前端页面的一种引擎 django 自带templates from django.http import HttpResponse ###渲染jinja2的一个类 from pyecharts.globals import CurrentConfig ###设置全局配置项 CurrentConfig.GLOBAL_ENV = Environment(loader=FileSystemLoader('templates')) ####配置html文件路径 ####配置 django 直接渲染pyecharts中的html模板 from pyecharts import options as opts from pyecharts.charts import Bar ####Bar 是柱状图 ''' 实现网站交互逻辑 指定html文件进行渲染 ''' ###视图 def hello(request): return HttpResponse('hello world') #渲染数据 def pyechart_demo(request): ''' :return:浏览器向网站后台发送的请求头 :param request: :return: ''' print(request) bar = ( Bar() .add_xaxis(['小米','华为','apple','samdd']) .add_yaxis('商家A',[55,33,44,22]) .add_yaxis('商家B',[25,3,99,111]) .add_yaxis('商家C',[5,3,9,11]) .set_global_opts(title_opts=opts.TitleOpts(title='shouji')) ) return HttpResponse(bar.render_embed()) ###render_embed 直接渲染到jiaja2里面