Skip to content
Django RestFramework API 项目模板
Python Dockerfile
Branch: master
Clone or download

Latest commit

xiaomastack
xiaomastack update README.md
Latest commit 61ff2af Dec 4, 2019

Files

Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
{{cookiecutter.project_name}}
.gitignore
README.md
cookiecutter.json

README.md

Django RestFramework API 项目模板

  • 支持签名认证
  • 支持token(过期)认证
  • 支持跨域
  • 支持swagger
  • 支持容器部署
  • 命令行操作使用ipython风格

使用方法

  • 安装cookiecutter
$ pip install cookiecutter
  • 使用模板生成项目
$ cookiecutter https://github.com/xiaomastack/django_api_template
# 或者
$ cookiecutter gh:xiaomastack/django_api_template
# 或者
$ cookiecutter git@github.com:xiaomastack/django_api_template.git
  • 安装环境
$ pip install -r requirements.txt
  • 初始化数据库
$ python manage.py makemigrations account
$ python manage.py migrate
  • 创建管理员用户
$ python manage.py createsuperuser
  • 为用户root生成签名密钥对
$ python manage.py genapikey root
api_key: root
api_secret: ATLV5DdHH9ZSsfAtyn6emz2YvqIb9KL4

接口认证使用httpsig.requests_auth.HTTPSignatureAuth

class BasicAuthHttp(object):
    def __init__(self, key=KEY, secret=SECRET):
        self.auth = HTTPSignatureAuth(key_id=key, secret=secret)
        self.headers = {
            'Content-Type': 'application/json',
            'X-Api-Key': key,
            'Date': datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        }

    def httpGet(self, url):
        return requests.get(url, auth=self.auth, headers=self.headers)
  • 通过接口获取token
$ curl -H "Content-Type: application/json" -X POST -d '{"username":"root","password":"123787Gemini"}' http://127.0.0.1:2000/token/
{"token":"51448e1f2265898a6ee63271a1019003a36710e8"}

接口认证头信息带上Authorization

Authorization -> Token a9992aa866ce447c80e1e3b00f3729e2 
  • 容器部署
# 编译镜像(380MB)
$ docker build --rm -t django_test:0.0.1 .
# 运行容器
$ docker run -itd -p 8080:8080 --name=django django_test:0.0.1
You can’t perform that action at this time.