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,配置,创建
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python实现字符串逆序输出功能示例
2023-10-14 04:59:17
Python seek()和tell()函数的具体使用
2023-12-07 20:59:39
![](https://img.aspxhome.com/file/2023/2/67242_0s.gif)
xml xpath基础语法
2008-01-21 12:46:00
在python中实现求输出1-3+5-7+9-......101的和
2022-10-08 16:33:14
python中urlparse模块介绍与使用示例
2021-08-08 10:13:18
详解利用django中间件django.middleware.csrf.CsrfViewMiddleware防止csrf攻击
2023-03-16 14:33:51
浅谈pytorch中的nn.Sequential(*net[3: 5])是啥意思
2023-10-05 05:42:55
![](https://img.aspxhome.com/file/2023/2/80672_0s.png)
ImageMagick FAQ 问题摘要
2008-10-21 13:14:00
使用 Python 实现简单的 switch/case 语句的方法
2021-02-02 09:10:16
ASP万用分页程序
2007-09-21 12:45:00
Python解析m3u8拼接下载mp4视频文件的示例代码
2022-04-22 13:20:16
![](https://img.aspxhome.com/file/2023/9/103179_0s.png)
python获取命令行输入参数列表的实例代码
2022-02-26 15:48:41
[译]“我心中的ebay”
2008-06-04 12:09:00
![](https://img.aspxhome.com/file/UploadPic/20086/4/200864121043106s.jpg)
如何在sae中设置django,让sae的工作环境跟本地python环境一致
2022-03-09 22:04:54
CSS技巧之圆角背景与三角形
2010-10-19 12:40:00
python使用tcp传输图片数据
2023-08-09 06:33:48
![](https://img.aspxhome.com/file/2023/6/86616_0s.jpg)
ASP调试方法图文教程
2008-01-26 19:32:00
![](https://img.aspxhome.com/file/UploadPic/20081/26/2008126195146249s.jpg)
python中的字典操作及字典函数
2023-02-06 05:17:42
python使用sqlite3时游标使用方法
2022-10-10 09:29:16
Python的进制转换和ASCLL转换你了解吗
2021-09-18 05:33:59