django rest_framework

 环境 django 1.6,rest_framework 3.3 ubuntu采用pip安装的rest_framework 按照例子一步步做下来 运行 提示filters.py第119行有错误forms.UUField找不到,查找了下好像上下问并没有使用UUIDFilter这个,注释掉 这个时候错误变成了No module named http_client 没道理一安装有这么多错误,Bing之发现有人说是版本不匹配,从服务起down了一个2.4.x下来setup了 错误变成了'module' object has no attribute 'UserViewSet' 错误还在继续明天接着调试好了再补充。

继续。

然后查看我引用的rest_framework里面的确有UserViewSet 再之后不知道是什么原因。我看了下官网的DEMO里不是叫rest_framework是不是我的APP名字不应该是rest_framework改成quickstart再次运行,居然可以了。输入http://localhost:8000/提示TemplateDoesNotExist at /users/  rest_framework/api.html看了下面的错误提示404连bing也不知道答案 是不是啊 之后看到有个人说加上如下这个

TEMPLATES = [
{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [
        'templates',
    ],'APP_DIRS': True, 
    'OPTIONS': {
        'context_processors': [
            'django.template.context_processors.debug',
            'django.template.context_processors.request',
            'django.contrib.auth.context_processors.auth',
            'django.contrib.messages.context_processors.messages',
        ],
    },
},]

结果结果还是不行啊,没反应啊,也许我们的环境以及django 跟rest_framework的版本不对导致我自己的版本上文已经交待了。于是我想既然404我干脆直接整个rest_framework copy过去,这时候可以运行了,界面还挺漂亮,不过curl的方法怎么也不对,我改了下可以了 正确的curl请求应该是酱紫 

 curl http://localhost:8000/users/ -u test:5660331
 返回的结果是Json格式 结果如下

  [{"url": "http://localhost:8000/users/3/", "username": "111", "email": "123@123.com", "groups": []}, {"url": "http://localhost:8000/users/2/", "username": "bentos", "email": "tianye206@163.com", "groups": []}, {"url": "http://localhost:8000/users/1/", "username": "test", "email": "1302816889@qq.com", "groups": []}]

到这里就成功了 也可以看做是rest_framework的入门篇,并没有官网的拷过来贴。大家如果有问题可以提出来

原文地址:https://www.cnblogs.com/EncryptingLife/p/5132126.html