zoukankan      html  css  js  c++  java
  • 关于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模板。

  • 相关阅读:
    在wepy里面使用redux
    使用es6的蹦床函数解决递归造成的堆栈溢出
    解决layui下拉选择框只能选择不能手动输入文字
    POJ 2230 Watchcow (欧拉回路)
    POJ 2337 Catenyms (欧拉回路)
    POJ 2513 Colored Sticks (欧拉回路 + 字典树 +并查集)
    HDU 3018 Ant Trip (欧拉回路)
    HDU 1023 Train Problem II (大数卡特兰数)
    HDU 2067 小兔的棋盘 (卡特兰数)
    HDU 3584 Cube (三维数状数组)
  • 原文地址:https://www.cnblogs.com/xiami303/p/2513479.html
Copyright © 2011-2022 走看看