Job Search Engine
928 浏览 5 years, 8 months
3.1 JOB API
版权声明: 转载请注明出处 http://www.codingsoho.com/JOB API
安装库文件
pip install djangorestframework
将'rest_framework'
添加到INSTALLED_APP
INSTALLED_APPS = (
...
'rest_framework',
)
创建路由和视图
在job_entry下面创建package api
创建视图文件 views.py
from rest_framework.generics import ListAPIView
from job_entry.models import JobEntry
class JobEntryListAPIView(ListAPIView):
queryset = JobEntry.objects.all()
urls.py
from django.conf.urls import url, include
from .views import (
JobEntryListAPIView,
)
urlpatterns = [
url(r'^$', JobEntryListAPIView.as_view(), name="list"),
]
同时job_entryp.urls添加入口路由
url(r'^api/jobentry/', include("job_entry.api.urls",namespace="jobentry-api")),
序列化对象
创建 serializer.py
from rest_framework import serializers
from job_entry.models import JobEntry
class JobEntrySerializer(serializers.ModelSerializer):
class Meta:
model = JobEntry
fields = [
"title",
"company",
"description"
]
加这个serializer到views里
from job_entry.api.serializers import JobEntrySerializer
class JobEntryListAPIView(ListAPIView):
queryset = JobEntry.objects.all()
serializer_class = JobEntrySerializer
报错
'CSRFCheck' object has no attribute 'process_request'
从rest_framework.settings里面注释掉授权相关的class配置
或者先指定为空
REST_FRAMEWORK = {
"DEFAULT_AUTHENTICATION_CLASSES" : []
}
访问http://127.0.0.1:8000/je/api/jobentry/即可以看到列表内容