• 关于djangorestframework


    今天学习心内求法的《Django实战系列15》,按照文中所述下载安装:

    下面用Django REST framework来实现购物车(Cart)的RESTful web service。
    第一步:安装
    官方文档说可以用pip或easy_install 安装,但是经过实测使用easy_install安装的不是最新版,会损失一些特性。所以建议用源代码的方式安装:
    从http://pypi.python.org/pypi/djangorestframework/0.3.2 下载v0.3.2,解压后$sudo python setup.py install
    
    第二步:配置
    在depot/settings.py的INSTALLED_APPS中加入:
    'djangorestframework',

    一切顺利。

    使用:

    1.在onlineProject中修改了settings.py:

    在INSTALLED_APPS中加入:
    'djangorestframework',

    2.在onlineProject下添加resource.py:

    from django.core.urlresolvers import reverse
    from djangorestframework.views import View
    from djangorestframework.resources import ModelResource
    from models import *
    
    class LineItemResource(ModelResource):
        model = LineItem
        fields = ('product', 'unit_price', 'quantity')
        def product(self, instance):
        return instance.product.title 

    3.修改onlineProject/urls.py(添加下面这句到patterns):

      (r'API/cart/items', ListOrCreateModelView.as_view(resource=LineItemResource)),

    4.访问:
    按道理说访问http://localhost:8000/depotapp/API/cart/items/ 就可以看到生成的RESTful API,但是我却出现了以下错误:

    ListOrCreateModelView  (1146, "Table 'myonlineshop.onlineshop_lineitem' doesn't exist")

    出现上述错误是因为更新model以后没有同步更新数据库,打开数据库onlineshop,创建onlineshop_lineitem表。然后再访问,依旧出现一个错误:

    AttributeError at /API/cart/items

    
    
    type object 'URLObject' has no attribute 'parse'
    
    
    Request Method: GET
    Request URL: http://127.0.0.1:8000/API/cart/items
    Django Version: 1.4
    Exception Type: AttributeError
    Exception Value:
    type object 'URLObject' has no attribute 'parse'
    
    
    Error during template rendering
    
    In template C:\Python27\lib\site-packages\djangorestframework-0.3.2-py2.7.egg\djangorestframework\templates\renderer.html, error at line 68
    type object 'URLObject' has no attribute 'parse'

     traceback中提示在这里出现了错误:

    貌似暂时找不到解决方案,网上也没收到。nie则推荐使用mako模板。

  • 相关阅读:
    前端H5
    nginx的location的匹配规则
    非旺玖原装的PL2303,请联系您的供货商
    Arduino 怎样添加第三方拓展
    Arduino的shiftOut函数作用
    arduino 的analogRead() 和analogWrite()
    composer安装TP的时候:错误提示:zsh: no matches found: 5.1.*
    Bootstrap3 模态框点击无效
    人人商城支付成功后在哪修改订单状态
    人人商城怎样判断订单是否支付
  • 原文地址:https://www.cnblogs.com/xiami303/p/2513479.html
Copyright © 2020-2023  润新知