DJango – 接口访问方法 Demo

  • Post author:
  • Post category:其他




目的

理解 Django 交互方法

test1

访问接口, 然后直接返回数据 (json)

test2

GET 与 POST 访问 [ 这里只介绍 GET, POST 同理 ]

test3

访问接口, 返回静态网页数据

test4

访问接口,返回动态网页,HTML 专用

test5

访问接口,返回动态网页,JAVASCRIPT 专用

test6

文件下载 (csv)

参考 CSV EXPORT 文档



说明

在使用 DJango 时,除了编写 pytonn 代码以外,还需要注意下面事情

需要配置路由 (

urls.py

) 定义接口

http://x.x.x.x/接口/


pyton 程序 (后端) 与网页程序 (前段) 是相互独立,但也可以配合工作



example

参考文件结构

.
├── demo                       <- 项目目录
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py                <- 路由文件
│   └── wsgi.py
├── manage.py
├── template                   <- 动态 html 位置
│   ├── test3.html
│   ├── test4.html
│   └── test5.html
└── tiweb                      <-- apps
    ├── __init__.py
    ├── migrations
    │   └── __init__.py
    └── views.py               <- python 代码



路由配置

工作方式

client -> uwsgi -> urls.py 
                     \-->  /test1/
                              \-->  python( 运算 )
           uwsgi  <---------------------|

参考

urls.py

from django.conf.urls import url
from tiweb import views

urlpatterns = [
    url(r'^test1/', views.Demo.test1),
    url(r'^test2/', views.Demo.test2),
    url(r'^test3/', views.Demo.test3),
    url(r'^test4/', views.Demo.test4),
    url(r'^test5/', views.Demo.test5),
]



参考代码



test1


views.py

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.shortcuts import render
from django.http import HttpRequest
from django.shortcuts import render,HttpResponse, render_to_response
import json
from django.core   import serializers

# Create your views here.

class Demo(object):

    @staticmethod
    def test1(request):
        data = {}
        data['report'] = 'it my data'
        data['code'] = '1001'
        sample = json.dumps(data)
        return HttpResponse(sample,content_type="application/json")



效果

test1



test2


views.py

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.shortcuts import render
from django.http import HttpRequest
from django.shortcuts import render,HttpResponse, render_to_response
import json
from django.core   import serializers

# Create your views here.

class Demo(object):

@staticmethod
def test2(request):
    if request.method == 'GET':
        name =  request.GET.get('name','not_define')
        id = request.GET.get('id','0')
        msg={}
        msg['id'] = id
        msg['name'] = name
    return HttpResponse(json.dumps(msg),content_type="application/json" )



效果

不带参数效果

test2_1

带参数效果

test2_2



test3


views.py

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.shortcuts import render
from django.http import HttpRequest
from django.shortcuts import render,HttpResponse, render_to_response
import json
from django.core   import serializers

# Create your views here.
class Demo(object):

@staticmethod
def test3(request):
    return render( request, 'test3.html')

test3.hmlt

<html>
<head>
<title>just for test3</title>
</head>
<body>
just for test3
</body>
</html>



效果

test3



test4


views.py

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.shortcuts import render
from django.http import HttpRequest
from django.shortcuts import render,HttpResponse, render_to_response
import json
from django.core   import serializers

# Create your views here.
class Demo(object):

@staticmethod
def test4(request):
    if request.method == 'GET':
        name =  request.GET.get('name','not_define')
        id = request.GET.get('id','0')
        msg={}
        msg['id'] = id
        msg['name'] = name
        msg['title'] = 'test4'
    return render( request, 'test4.html', { 'data': msg } )

test4.html

<html>
<head>
<title>just for test4</title>
</head>
<body>
<table>
  <tr>
    <th align=right width=150>变量</th><th align=right width=150>值</th>
  </tr>
  {% for key, value in data.items %}
  <tr>
    <td align=right>{{ key }}</td><td align=right>{{ value }}</td>
  </tr>
  {% endfor %}
</table>
</body>
</html>



效果

不带参数效果

test4_1

带参数效果

tet4_2



test5


views.py

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.shortcuts import render
from django.http import HttpRequest
from django.shortcuts import render,HttpResponse, render_to_response
import json
from django.core   import serializers

# Create your views here.
class Demo(object):

@staticmethod
def test5(request):
    if request.method == 'GET':
        name =  request.GET.get('name','not_define')
        id = request.GET.get('id','0')
        msg={}
        msg['id'] = id
        msg['name'] = name
        msg['title'] = 'test5'
    return render( request, 'test5.html', { 'data': json.dumps(msg) } )

test5.html

<html>
<head>
<title>just for test4</title>
  <script type="text/javascript">
    var data = {{ data|safe  }};
    var id = data.id;
    var name = data.name;
    var title = data.title;
    function varFunction() {
        document.getElementById("id").innerHTML = id;
        document.getElementById("name").innerHTML = name;
        document.getElementById("title").innerHTML = title;
    }
  </script>
</head>
<body onload="varFunction()">
 <table>
  <tr>
     <th align=right width=150>变量名</th>
     <th align=right width=150>值</th>
  </tr>
  <tr>
     <td align=right>ID</td>
     <td align=right><span id="id"></span></td>
  </tr>
   <tr>
     <td align=right>NAME</td>
     <td align=right><span id="name"></span></td>
  </tr>
   <tr>
     <td align=right>TITLE</td>
     <td align=right><span id="title"></span></td>
  </tr>
 </table>
</body>
</html>



效果

不带参数效果

test5_1

带参数效果

test5_2

markdown plugin:


https://github.com/knsv/mermaid



https://vxhly.github.io/2018/05/markdown-mermaid-flow-chart/



https://support.typora.io/Draw-Diagrams-With-Markdown/



https://www.cnblogs.com/wuyida/p/6301240.html



版权声明:本文为signmem原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。