python+django+rest框架配置創(chuàng)建方法
安裝好所需要的插件和包:
python、django、pip等版本如下:

采用Django REST框架3.0
1、在python文件夾下D:\python\Lib\site-packages\django\bin打開cmd命令工具,本人將python文件夾名字改為了wwj,請(qǐng)注意:
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創(chuàng)建文件serializers.py,并寫入一下內(nèi)容:
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。設(shè)置模塊將處于tutorial/settings.py

6、通過python manage.py runserver啟動(dòng)框架

7、通過http://localhost:8000/在瀏覽器里打開

以上這篇python+django+rest框架配置創(chuàng)建方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
python遍歷目錄下所有文件的五種實(shí)現(xiàn)方法
本文主要介紹了python遍歷目錄下所有文件的五種實(shí)現(xiàn)方法,包含os.walk(),os.scandir(),os.listdir(),glob模塊和osqp模塊這幾種方法,具有一定的參考價(jià)值,感興趣的可以了解一下2024-07-07
基于python list對(duì)象中嵌套元組使用sort時(shí)的排序方法
下面小編就為大家分享一篇基于python list對(duì)象中嵌套元組使用sort時(shí)的排序方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-04-04
Python 字符串大小寫轉(zhuǎn)換的簡(jiǎn)單實(shí)例
下面小編就為大家?guī)硪黄狿ython 字符串大小寫轉(zhuǎn)換的簡(jiǎn)單實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-01-01
Python學(xué)習(xí)筆記整理3之輸入輸出、python eval函數(shù)
這篇文章主要介紹了Python學(xué)習(xí)筆記整理3之輸入輸出、python eval函數(shù)的相關(guān)資料,需要的朋友可以參考下2015-12-12
python numpy 部分排序 尋找最大的前幾個(gè)數(shù)的方法
今天小編就為大家分享一篇python numpy 部分排序 尋找最大的前幾個(gè)數(shù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-06-06
python裝飾器-限制函數(shù)調(diào)用次數(shù)的方法(10s調(diào)用一次)
下面小編就為大家分享一篇python裝飾器-限制函數(shù)調(diào)用次數(shù)的方法(10s調(diào)用一次),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-04-04
基于Python實(shí)現(xiàn)語(yǔ)音識(shí)別功能
這篇文章給大家介紹了如何基于Python實(shí)現(xiàn)語(yǔ)音識(shí)別功能,文中通過代碼示例給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,具有一定的參考價(jià)值,需要的朋友可以參考下2023-12-12

