码迷,mamicode.com
首页 > 其他好文 > 详细

Django-rest-framework 的使用

时间:2017-05-10 00:18:45      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:org   pattern   view   app   logs   ref   stand   mini   return   

安装:

 

Install using pip, including any optional packages you want...

pip install djangorestframework
pip install markdown       # Markdown support for the browsable API.
pip install django-filter  # Filtering support

 

获取代码

 

...or clone the project from github.

git clone git@github.com:tomchristie/django-rest-framework.git

 

setting 配置

INSTALLED_APPS = (
    ...
    rest_framework,
)


REST_FRAMEWORK = {
    # Use Django‘s standard `django.contrib.auth` permissions,
    # or allow read-only access for unauthenticated users.
    ‘DEFAULT_PERMISSION_CLASSES‘: [
        ‘rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly‘
    ]
}

If youre intending to use the browsable API youll probably also want to add REST frameworks login and logout views. Add the following to your root urls.py file. urlpatterns = [ ... url(r^api-auth/, include(rest_framework.urls, namespace=rest_framework)) ]

Example

url配置


from rest_framework import routers
from crm.rest_views import UserViewSet,RoleViewSet
# Routers provide an easy way of automatically determining the URL conf.
  router = routers.DefaultRouter()
  router.register(r‘users‘, UserViewSet)
  router.register(r‘roles‘, RoleViewSet)


# Wire up our API using automatic URL routing.
# Additionally, we include login URLs for the browsable API.
urlpatterns = [
    url(r‘^api/‘, include(router.urls)),
 url(r‘^apitest/‘, views.api_test  ),
url(r^api-auth/, include(rest_framework.urls, namespace=rest_framework)) ]
rest_serializer配置
数据序列化

rom crm import models
from rest_framework import serializers

# Serializers define the API representation.
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = models.UserProfile
depth = 3
fields = (‘url‘, ‘email‘, ‘name‘, ‘is_staff‘,‘is_active‘,‘role‘)


class RoleSerializer(serializers.ModelSerializer):
class Meta:
model = models.Role
fields = ("name",)

如果是外检 需要导入表
rest_views配置
from crm import models from crm.rest_serializer import UserSerializer,RoleSerializer from rest_framework import viewsets class UserViewSet(viewsets.ModelViewSet): queryset = models.UserProfile.objects.all() # 取到数据 serializer_class = UserSerializer # 序列化 class RoleViewSet(viewsets.ModelViewSet): queryset = models.Role.objects.all() serializer_class = RoleSerializer

views 视图
def api_views(request):
if request.method=="POST":
data = json.loads(request.POST.get(‘data‘))
print(data)
serialize_obj=rest_serializer.UserSerializer(data=data)
if serialize_obj.is_valid():
serialize_obj.save()

return render(request,‘crm/api-test.html‘,locals())

 

api_test.html

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form method="POST"> {% csrf_token %} <textarea name="data" cols="88" rows="6"></textarea> <input type="submit" > </form> {{ serialize_obj }} </body> </html>

初步研究 

 

 

官网文档  http://www.django-rest-framework.org/

 

 

 

Django-rest-framework 的使用

标签:org   pattern   view   app   logs   ref   stand   mini   return   

原文地址:http://www.cnblogs.com/honglingjin/p/6833341.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!