django 访问数据库返回 JSON数据格式

from django.shortcuts import render

# Create your views here.

from django.shortcuts import render
from django.http import HttpResponse
from django.contrib.auth.decorators import login_required
from django.shortcuts import render_to_response

# Create your views here.
import cx_Oracle as oracle
import json
def search_db(sqlcmd):
    conn = oracle.connect('test/test@test:1526/orcl')
    cur  = conn.cursor()
    cur.execute(sqlcmd)
    result = cur.fetchall()
#    print ("Exec sql:%s"%(sqlcmd))
    conn.close()
    return result
def cont_json_format(dbname, USED_SIZE, FREE_SIZE,SM_SIZE,OTIME):
    json_str = {"code":200, "message": "OK", "descriptions": {"DB_NAME":dbname, "USED_SIZE": USED_SIZE, "FREE_SIZE": FREE_SIZE, "SM_SIZE":SM_SIZE, "OTIME":OTIME}}
    return json.dumps(json_str)

def cont(request,dbname):
    starttime = request.GET['starttime']
    endtime = request.GET['endtime']
    sql='''select * 
           from ORADB_SIZE t
           where 
            t.DB_NAME = '%s' and 
            t.OTIME > to_date('%s','YYYY/MM/DD') and 
            t.OTIME < to_date('%s','YYYY/MM/DD')''' % (dbname, starttime, endtime)
    result = search_db(sql)
    USED_SIZE = [round(e[1],2) for e in result]
    FREE_SIZE = [round(e[2],2) for e in result]
    SM_SIZE = [round(e[3],2) for e in result]
    OTIME = [e[4].strftime('%Y-%m-%d') for e in result]
    json_data = cont_json_format(dbname, USED_SIZE, FREE_SIZE, SM_SIZE, OTIME)
    return HttpResponse(json_data)


def echarts_cont(request):
    return render_to_response("content.html")

转载于:https://my.oschina.net/zhiyonghe/blog/920475

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用Django提供的序列化器(Serializer)将数据库数据转换为JSON格式文件。 首先,你需要在Django中定义一个模型,并将其注册到admin.py中。例如,我们有一个名为Book的模型: ```python from django.db import models class Book(models.Model): title = models.CharField(max_length=200) author = models.CharField(max_length=200) publication_date = models.DateField() def __str__(self): return self.title ``` 然后,在admin.py中注册该模型: ```python from django.contrib import admin from .models import Book @admin.register(Book) class BookAdmin(admin.ModelAdmin): list_display = ('title', 'author', 'publication_date') ``` 接下来,我们可以使用Django提供的序列化器(Serializer)将该模型的数据转换为JSON格式文件。在views.py中,我们可以编写以下代码: ```python from django.core import serializers from django.http import HttpResponse def export_books_to_json(request): books = Book.objects.all() data = serializers.serialize('json', books) response = HttpResponse(data, content_type='application/json') response['Content-Disposition'] = 'attachment; filename="books.json"' return response ``` 在上述代码中,我们首先从数据库中获取所有的书籍数据,然后使用Django提供的序列化器将数据转换为JSON格式字符串。最后,我们将JSON字符串作为HttpResponse返回,并设置Content-Disposition标头以将其作为文件下载。 你可以在urls.py中添加以下代码以创建一个URL,该URL将触发export_books_to_json视图: ```python from django.urls import path from .views import export_books_to_json urlpatterns = [ path('export/', export_books_to_json, name='export_books_to_json'), ] ``` 当你访问该URL时,将自动下载books.json文件,其中包含所有书籍数据的JSON格式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值