接口开发转载自【秦无殇的博客】:https://www.cnblogs.com/webDepOfQWS/p/10693152.html
原文章贴在下面作为笔记,全程按照原文走下来,加上下面的各种错误的解决,顺便记录一下遇到的问题及解决方案:
各版本如下:
centos:7.2
python:3.6.8(win10下是3.7,实测没问题)
django:2.2
pymysql:0.9.3
先在win上进行的调试
【记录1】:setting.py文件中
ALLOWED_HOSTS = ['*'] # 允许任何IP进行访问
DATABASES = {
# 'default': {
# 'ENGINE': 'django.db.backends.sqlite3',
# 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
# },
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': '数据库名称',
'USER': '用户名',
'PASSWORD': '密码',
'HOST': '数据库IP',
'PORT': '3306',
}
}
# 设置中文
# LANGUAGE_CODE = 'en-us'
LANGUAGE_CODE = 'zh-hans'
# TIME_ZONE = 'UTC'
TIME_ZONE = 'Asia/Shanghai'
【记录2】:admin.py文件中
配置admin管理后台的显示
from . import models # 导入当前文件夹下的models文件
admin.site.register(models.User), #
admin.site.register(models.Article),
【问题1】连接mysql时总是提示版本不正确,
解决:修改源码中的文件,
$Anaconda3\Lib\site-packages\django\db\backends\mysql\base.py
注释35和36行如下:
# if version < (1, 3, 13):
# raise ImproperlyConfigured('mysqlclient 1.3.13 or newer is required; you have %s.' % Database.__version__)
修改源码:
$Anaconda3\Lib\site-packages\django\db\backends\mysql\operations.py
146行修改为:
if query is not None:
query = query.encode(errors='replace')
return query
【以下为原文】
pip install django
进入项目代码存放目录执行命令:
django-admin.py startproject blog_demo
进入blog_demo,运行命令:
python3.6 manage.py runserver 9000
在浏览器地址栏打开:http://127.0.0.1:9000/ 如果出现以下画面,则说明服务器正在运行
django中每一个app可以看作是一个模块,以app为单位,结构清晰,方便管理。
python3.6 manage.py startapp blog_api
编写模型层代码,以下语句相当于创建了两张表:User,Article
class User(models.Model):
id = models.AutoField(primary_key=True)
uname = models.CharField(max_length=50)
upwd = models.CharField(max_length=100)
#active inactive
status = models.CharField(max_length=10)
class Article(models.Model):
id = models.AutoField(primary_key=True)
title = models.CharField(max_length=50)
content = models.TextField()
#deleted alive
status = models.CharField(max_length=10)
创建表结构:
python3.6 manage.py migrate
settings.py文件INSTALLED_APPS处新增app:blog_api
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'blog_api'
]
让django知道模型有了变化:
python3.6 manage.py makemigrations blog_api
再次创建表结构:
python3.6 manage.py migrate
在浏览器控制台输入:http://127.0.0.1:9000/admin/login/?next=/admin/
stephen@stephen-K55VD:~/IdeaProjects/blog_demo$ python3.6 manage.py createsuperuser
Username (leave blank to use 'stephen'): admin
Email address:
Password:
Password (again):
This password is too common.
Bypass password validation and create user anyway? [y/N]: y
Superuser created successfully.
邮件地址可以不填,注册成功后即可登录。使用admin后台来管理模型需要先注册,修改blog_api/admin.py代码
#导入模型User,Article
from blog_api.models import User,Article
admin.site.register(User)
admin.site.register(Article)
from blog_api.views import add_article,modify_article
urlpatterns = [
path('admin/', admin.site.urls),
path('articles/',add_article),
path('articles/',modify_article)
]
from django.http import HttpResponse,JsonResponse
from blog_api.models import User,Article
import json
#新增文章
def add_article(request):
if request.method == "POST":
req = json.loads(request.body)
print (req)
key_flag = req.get("title") and req.get("content") and len(req)==2
#判断请求体是否正确
if key_flag:
title = req["title"]
content = req["content"]
#title返回的是一个list
title_exist = Article.objects.filter(title=title)
#判断是否存在同名title
if len(title_exist) != 0:
return JsonResponse({"status":"BS.400","msg":"title aleady exist,fail to publish."})
'''插入数据'''
add_art = Article(title=title,content=content,status="alive")
add_art.save()
return JsonResponse({"status":"BS.200","msg":"publish article sucess."})
else:
return JsonResponse({"status":"BS.400","message":"please check param."})
#查询所有文章和状态
if request.method == "GET":
articles = {}
query_art = Article.objects.all()
for title in query_art:
articles[title.title] = title.status
return JsonResponse({"status":"BS.200","all_titles":articles,"msg":"query articles sucess."})
#修改文章
def modify_article(request,art_id):
if request.method == "POST":
req = json.loads(request.body)
try:
art = Article.objects.get(id=art_id)
key_flag = req.get("title") and req.get("content") and len(req)==2
if key_flag:
title = req["title"]
content = req["content"]
title_exist = Article.objects.filter(title=title)
if len(title_exist) > 1:
return JsonResponse({"status":"BS.400","msg":"title aleady exist."})
'''更新数据'''
old_art = Article.objects.get(id=art_id)
old_art.title = title
old_art.content = content
old_art.save()
return JsonResponse({"status":"BS.200","msg":"modify article sucess."})
except Article.DoesNotExist:
return JsonResponse({"status":"BS.300","msg":"article is not exists,fail to modify."})
#删除文章
if request.method == "DELETE":
try:
art = Article.objects.get(id=art_id)
art_id = art.id
art.delete()
return JsonResponse({"status":"BS.200","msg":"delete article sucess."})
except Article.DoesNotExist:
return JsonResponse({"status":"BS.300","msg":"article is not exists,fail to delete."})
四个简单的接口已经可以运行了,但是在发请求之前没有进行鉴权,毫无安全性可言。下面来实现简单的认证机制。需要用到内建模块hashlib,hashlib提供了常见的摘要算法,如MD5,SHA1等。
新增一个专门用于鉴权的接口。在urls.py中添加
path("auth/",get_token)
在views.py前面新增函数get_token(request)
import hashlib
#获取token
def get_token(request):
req = json.loads(request.body)
uname = req["username"]
upwd = req["password"]
if request.method == "POST":
try:
tmppwd =User.objects.get(uname=uname).upwd
if upwd == tmppwd:
md5 = hashlib.md5()
#把密码变成一个长度固定的字符串
md5.update(upwd.encode("utf-8"))
return JsonResponse({"status":"BS.201","X-Token":md5.hexdigest()})
else:
return JsonResponse({"status":"BS.401","msg":"username or password may wrong."})
except User.DoesNotExist:
return JsonResponse({"status":"BS.500","msg":"username is not exist."})
登录django admin在blog_api下的User表新增一条记录。运行结果:
request.META.get(“header key”) 用于获取header的信息。注意的是header key必须增加前缀HTTP,同时大写,中划先会转成下划线,例如你的key为X-Token,那么应该写成request.META.get(“HTTP_X_TOKEN”),修改views.py在get_token后面加上如下代码:
#认证动作
def user_auth(request):
token = request.META.get("HTTP_X_TOKEN",b'')
print (token)
if token:
#暂时先写上auth接口返回的数据
if token=="0a6db4e59c7fff2b2b94a297e2e5632e":
return "auth_sucess"
else:
return "auth_fail"
else:
return "auth_fail"
在接口中调用user_auth函数,以发布文章接口为例:
#新增文章
def add_article(request):
auth_res = user_auth(request)
if auth_res == "auth_fail":
return JsonResponse({"status":"BS.401","msg":"user auth failed."})
else:
if request.method == "POST":
req = json.loads(request.body)
print (req)
.......