一、安装python2.7.18

image.png
2.配置环境变量

image.png
3.验证是否安装成功

image.png
二、搭建环境
1、安装django
pip install django
2.新建项目
django-admin startproject myproject
3.新建app
python manage.py startapp server
4.项目结构目录

image.png
5.安装djangorestframework
pip install djangorestframework
6.配置
添加rest_framework到INSTALLED_APPS
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'server',
'rest_framework',
]
server目录下新建一个urls.py作为路由
from django.contrib import admin
from django.conf.urls import url, include
from server import views
urlpatterns = [
url('test/', views.test)
]
在views.py里新建一个方法
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.decorators import api_view
from django.http import JsonResponse, HttpResponseNotAllowed, HttpResponse
# Create your views here.
@api_view(['GET'])
def test(request):
if request.method == 'GET':
content = {'msg': 'SUCCESS'}
return JsonResponse(data=content, safe=False)
修改根目录下面的urls.py
"""myproject URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/1.11/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: url(r'^$', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.conf.urls import url, include
2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls'))
"""
from django.conf.urls import url,include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url('', include('server.urls')),
]
三.设置用自己的iP地址访问项目
1.首先执行
manage.py runserver 0.0.0.0:8000

image.png
2.在setting.py里面需要修改ALLOWED_HOSTS="*"

image.png
四.验证接口

image.png
参考连接:https://juejin.cn/post/6844904035426762759
网友评论