Django 配置restframework与配置settings.py文件

# pip install djangorestframework

setting.py中配置

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework',  # 配置restful api需要配置的
    'app'
]

# 配置时区
LANGUAGE_CODE = 'zh-hans'
TIME_ZONE = 'Asia/Shanghai'
# 不设置为False时间入库会报警告
USE_TZ = False

urls.py中需要配置

from django.conf.urls import url, include
urlpatterns = [
    path('admin/', admin.site.urls),
    url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')),
    url(r'^student/', include('app.urls')),    # 自己应用配置的urls
]

 views.py中需要配置

from rest_framework.decorators import api_view, permission_classes
from rest_framework.permissions import AllowAny
from rest_framework.response import Response

@api_view(('GET',))
@permission_classes((AllowAny,))
def getdata(request):
    results = {}
    status = 200
    return Response(data=results, status=status)

 

 

你可能感兴趣的:(Django)