【python】tastypie--django restful风格接口设计

一、安装tastypie:

[C:\Users\jrzhaoxueyong\git\JDJR\vpc\controller]$ python -m pip install django-tastypie
Collecting django-tastypie
  Downloading django_tastypie-0.13.3-py2.py3-none-any.whl (75kB)
Collecting python-dateutil!=2.0,>=1.5 (from django-tastypie)
  Downloading python_dateutil-2.6.0-py2.py3-none-any.whl (194kB)
Collecting python-mimeparse!=1.5,>=0.1.4 (from django-tastypie)
  Downloading python_mimeparse-1.6.0-py2.py3-none-any.whl
Collecting six>=1.5 (from python-dateutil!=2.0,>=1.5->django-tastypie)
  Using cached six-1.10.0-py2.py3-none-any.whl
Installing collected packages: six, python-dateutil, python-mimeparse, django-tastypie
Successfully installed django-tastypie-0.13.3 python-dateutil-2.6.0 python-mimeparse-1.6.0 six-1.10.0

二、添加到django项目app列表:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'tastypie',
]

三、安装lxml:

> python -m pip install lxml
error: Microsoft Visual C++ 9.0 is required. Get it from http://aka.ms/vcpython27
# 安装Microsoft Visual C++ Compiler for Python 2.7
# http://origin.www.ms.akadns[.NET](http://lib.csdn.net/base/dotnet)/en-us/download/details.aspx?id=44266
> python -m pip install lxml
# Could not find function xmlCheckVersion in library libxml2. Is libxml2 installed?
# https://pypi.python.org/pypi/lxml/2.3/#downloads
# 执行安装文件即可
[c:\~]$ pip install lxml
Requirement already satisfied: lxml in c:\python27\lib\site-packages

四、添加资源接口:

资源接口目录

开发资源接口代码如下:

class AllGatwayResource(ModelResource):
    class Meta:
        queryset = models.NatGw.objects.all()
        allowed_methods = ['get']
        resource_name = 'gateway'
        filtering = {
            'type': ('natgw',),
            }
        
    def get_list(self, request, **kwargs):
        return self.create_response(request, **kwargs)

五、启动服务:

[D:\EldonZhao\git\JDJR\vpc\controller]$ python manage.py runserver

六、接口访问:

调用http://localhost:8000/api/v1/?format=json

调用结果

参考资料:

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,027评论 19 139
  • # Python 资源大全中文版 我想很多程序员应该记得 GitHub 上有一个 Awesome - XXX 系列...
    aimaile阅读 26,611评论 6 427
  • 经过对django的初步学习,我们已经对后台的基本流程以及django的运作有了一定的了解,但是这还不足够,dja...
    coder_ben阅读 3,861评论 8 34
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,976评论 6 342
  • 情绪有点激动了,明白再多道理,还是抚不平生活里的鸡零狗碎。 那怎么办呢?再用最笨的办法自己理一理事情的始终。 回家...
    俏村姑阅读 188评论 0 0