Django-rest-framework 的使用

  • 时间:
  • 来源:互联网
  • 文章标签:

安装:

 

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 you're intending to use the browsable API you'll probably also want to add REST framework's 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/

 

 

 

转载于:https://www.cnblogs.com/honglingjin/p/6833341.html

本文链接http://www.taodudu.cc/news/show-647654.html