创建django项目

首选下载

pip install django==3.2.19,等号后面是版本号

然后我们去创建

复制代码
django-admin startproject  django

然后配置app

复制代码
cd django

python manage.py startapp app

然后运行

复制代码
python  manage.py runserver

然后我们设置,去sttings.py里面添加刚刚建的app

复制代码
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'app',

]

然后我们写第一个接口

安装restframework

复制代码
pip install djangorestframework

挂在

复制代码
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'app',
    'rest-framework'

]

在app文件下下面的views文件里写一下代码

复制代码
from  rest_framework.decorators import api_view

from   rest_framework.response import Response


@api_view(["GET"])
def frist_api():
    
   return Response({"data":"ok"})

去django文件夹里面的urls里面配置一下代码

复制代码
path('api/', views.frist_api),

完成了我们第一个接口

然后我们配置跨域

我们用一个第三方库,先下载安装

复制代码
pip install django-cors-headers
挂在
INSTALLED_APPS = [
    ...,
    "corsheaders",
    ...,
]

配置这里
MIDDLEWARE = [
    ...,
    "corsheaders.middleware.CorsMiddleware",
    "django.middleware.common.CommonMiddleware",
    ...,
]
相关推荐
dagouaofei3 分钟前
PPT AI生成实测报告:哪些工具值得长期使用?
人工智能·python·powerpoint
冒泡的肥皂10 分钟前
AI小应用分享
人工智能·后端
BoBoZz1911 分钟前
ExtractPolyLinesFromPolyData切割一个三维模型(球体),并可视化切割后产生的多条等高线
python·vtk·图形渲染·图形处理
quikai198122 分钟前
python练习第六组
java·前端·python
阿虎儿22 分钟前
本地部署docker完整版minIO镜像
后端
亚当24 分钟前
SpringBoot中使用MyBatis入门笔记
后端
诺斯贝克35 分钟前
Unable to create converter for xxx.NetworkResponse<Auth> for method AuthService
前端·后端
Trouville0136 分钟前
Python中encode和decode的用法详解
开发语言·python
用户693717500138437 分钟前
29.Kotlin 类型系统:智能转换:类型检查 (is) 与类型转换 (as)
android·后端·kotlin
用户693717500138437 分钟前
30. Kotlin 扩展:为“老类”添“新衣”:扩展函数与扩展属性
android·后端·kotlin