python+django+rest框架配置創(chuàng)建方法
安裝好所需要的插件和包:
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創(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啟動框架
7、通過http://localhost:8000/在瀏覽器里打開
以上這篇python+django+rest框架配置創(chuàng)建方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python中單引號、雙引號和三引號具體的用法及注意點(diǎn)
這篇文章主要給大家介紹了關(guān)于Python中單引號、雙引號和三引號具體的用法及注意點(diǎn)的相關(guān)資料,Python中單引號、雙引號、三引號中使用常常困惑,想弄明白這三者相同點(diǎn)和不同點(diǎn),需要的朋友可以參考下2023-07-07Mac更新python3.12?解決pip3安裝報錯問題小結(jié)
Mac使用homebrew更新了python3.12,刪除了以前的版本和pip3安裝軟件時候報錯,下面小編給大家分享Mac更新python3.12?解決pip3安裝報錯問題,感興趣的朋友跟隨小編一起看看吧2024-05-05Python面向?qū)ο髮?shí)現(xiàn)方法總結(jié)
這篇文章主要介紹了Python面向?qū)ο髮?shí)現(xiàn)方法總結(jié),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-08-08django 多數(shù)據(jù)庫及分庫實(shí)現(xiàn)方式
這篇文章主要介紹了django 多數(shù)據(jù)庫及分庫實(shí)現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04