DRF纯净版项目搭建和配置

一、安装模块和项目

1.安装模块

pip install django
pip install djangorestframework
pip install django-redis # 按需安装

2.开启项目和api

(venv) PS D:\pythonProject\env_api> django-admin startproject drf .
(venv) PS D:\pythonProject\env_api> python manage.py startapp api

DRF纯净版项目搭建和配置_第1张图片

二、注册app和restframework

1.修改settings.py

INSTALLED_APPS = [
    # 'django.contrib.admin',
    # 'django.contrib.auth',
    # 'django.contrib.contenttypes',
    # 'django.contrib.sessions',
    # 'django.contrib.messages',
    'django.contrib.staticfiles',
    'api.apps.ApiConfig',
    'rest_framework',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    # 'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    # 'django.middleware.csrf.CsrfViewMiddleware',
    # 'django.contrib.auth.middleware.AuthenticationMiddleware',
    # 'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        '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',
            ],
        },
    },
]


################# DRF配置 ##################
REST_FRAMEWORK = {
    "UNAUTHENTICATED_USER": None
}

2.新建urls参数配置

from django.urls import path
from api import views
urlpatterns = [
    path('home/', views.HomeView.as_view()),
]
  1. views.py新建视图类
from rest_framework.views import APIView
from rest_framework.response import Response

# Create your views here.

class HomeView(APIView):
    def get(self, request):
        return Response("...")

4.页面展示

DRF纯净版项目搭建和配置_第2张图片

你可能感兴趣的:(Python,sqlite,数据库)