python+django+rest框架配置创建方法
作者:weijun-w 时间:2021-10-07 02:28:52
安装好所需要的插件和包:
python、django、pip等版本如下:
采用Django REST框架3.0
1、在python文件夹下D:\python\Lib\site-packages\django\bin打开cmd命令工具,本人将python文件夹名字改为了wwj,请注意:
mkdir tutorial
cd tutorial
virtualenv env
source env/bin/activate
pip install django
pip install djangorestframework
django-admin startproject tutorial .
cd tutorial
django-admin startapp quickstart
cd ../
2、
python manage.py migrate
python manage.py createsuperuser
3、在tutorial\quickstart创建文件serializers.py,并写入一下内容:
from django.contrib.auth.models import User, Group
from rest_framework import serializers
class UserSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = User
fields = ('url', 'username', 'email', 'groups')
class GroupSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Group
fields = ('url', 'name')
3、tutorial\quickstart\views.py中写入:
from django.contrib.auth.models import User, Group
from rest_framework import viewsets
from tutorial.quickstart.serializers import UserSerializer, GroupSerializer
class UserViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows users to be viewed or edited.
"""
queryset = User.objects.all().order_by('-date_joined')
serializer_class = UserSerializer
class GroupViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows groups to be viewed or edited.
"""
queryset = Group.objects.all()
serializer_class = GroupSerializer
4、tutorial\urls.py中写入:
from django.conf.urls import url, include
from rest_framework import routers
from tutorial.quickstart import views
router = routers.DefaultRouter()
router.register(r'users', views.UserViewSet)
router.register(r'groups', views.GroupViewSet)
# Wire up our API using automatic URL routing.
# Additionally, we include login URLs for the browsable API.
urlpatterns = [
url(r'^', include(router.urls)),
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
]
5、添加'rest_framework'到INSTALLED_APPS。设置模块将处于tutorial/settings.py
6、通过python manage.py runserver启动框架
7、通过http://localhost:8000/在浏览器里打开
来源:https://blog.csdn.net/qq_37705048/article/details/79896507
标签:python,django,rest,配置,创建
0
投稿
猜你喜欢
基于Token的身份验证的方法
2023-09-27 19:05:41
python事件驱动event实现详解
2021-07-27 21:17:04
Golang实现请求限流的几种办法(小结)
2024-05-05 09:32:09
Python进程间通信 multiProcessing Queue队列实现详解
2021-06-17 18:53:59
Python read函数按字节(字符)读取文件的实现
2021-08-20 09:40:48
python3安装pip3(install pip3 for python 3.x)
2023-03-30 07:19:09
Android申请相机权限和读写权限实例
2023-08-21 18:52:40
win10 安装mysql 8.0.18-winx64的步骤详解
2024-01-24 06:54:04
Python实现简易信息分类存储软件
2023-08-09 20:45:08
解读python如何实现决策树算法
2021-08-26 19:52:01
教你为MySQL数据库换挡加速
2010-03-03 16:58:00
使用Go实现优雅重启服务功能
2024-04-25 15:06:06
css网页下拉菜单制作方法(2):初步实现
2007-02-03 11:39:00
python读写二进制文件的方法
2023-04-30 15:28:41
ecshop百度编辑器远程下载无后缀的图片,并且加水印
2023-08-14 17:31:41
Python获取当前脚本文件夹(Script)的绝对路径方法代码
2021-05-27 09:57:52
千分位数字格式化(用逗号隔开 代码已做了修改 支持0-9位逗号隔开)的JS代码
2023-08-19 20:33:57
Python sklearn对文本数据进行特征化提取
2023-05-19 09:07:04
Go+Vue开发一个线上外卖应用的流程(用户名密码和图形验证码)
2024-05-09 14:52:17
在Windows的Apache服务器上配置对PHP和CGI的支持
2023-10-20 22:12:17