美文网首页我的Python自学之路
tastypie——django restful风格接口设计

tastypie——django restful风格接口设计

作者: EldonZhao | 来源:发表于2017-02-27 14:19 被阅读170次

    一、安装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

    调用结果

    参考资料:

    相关文章

      网友评论

        本文标题:tastypie——django restful风格接口设计

        本文链接:https://www.haomeiwen.com/subject/vrhbwttx.html