Skip to main content

Django-API-Utils是基于Django编写后端API的一些组件和工具,致力于努力探究 Django 最佳实践。

Project description

项目说明

Django-API-Utils是基于Django编写后端API的一些组件和工具,致力于努力探究 Django 最佳实践。

目前基于 ITM-0011 中的规范编写,仍待完善和在实践中优化。

环境依赖

目前已在 Django 3 下测试可以成功运行。

环境要求:

Django >= 3.2, djangorestframework >= 3.12 , djangorestframework-simplejwt >= 5.0.0 (如果不使用认证组件,将auth.py删除即可,不必安装此模块)

初始化项目

  1. 下载本项目源码,将utils文件夹拖到项目根目录下

    下载本项目源码可以使用git工具、直接在Github上Download ZIP、或者下载发布的release包。

  2. 新建apps目录,将项目内的app放入apps文件夹内。

  3. settings.py文件内,新增apps目录到BASE_DIR

    ...
    BASE_DIR = Path(__file__).resolve().parent.parent
    # 新增下面这一行
    sys.path.insert(0, os.path.join(BASE_DIR, 'apps'))
    ...
    
  4. 在根路由文件urls.py下初始化Router Build

    from utils.router_builder import RouterBuilder
    router = RouterBuilder()
    urlpatterns = [
        path("api/", include(router.urls)),
        path("api/", include(router.url_patterns)),
    ]
    
  5. (非必需)在项目根目录下新建config.json文件,将项目的一些配置信息和敏感信息写到json文件中,并在settings.py中读取

    import json
    
    # Configuration File
    with open('./conf.json', 'r') as f:
        config = json.load(f)
    
    SECRET_KEY = config.get('SECRET_KEY','')
    
    MYSQL_DB_NAME = config.get('MYSQL_DB_NAME','')
    MYSQL_DB_USER = config.get('MYSQL_DB_USER','')
    MYSQL_DB_PASSWORD = config.get('MYSQL_DB_PASSWORD','')
    

    这样的好处在于,避免将敏感信息暴露到Git服务器上。

初始化完后,项目的目录树大概是这样的:

.
├── apps
│   ├── __init__.py
│   └── <app_name>
│       ├── __init__.py
│       ├── admin.py
│       ├── apps.py
│       ├── migrations
│       │   └── __init__.py
│       ├── models.py
│       ├── serializers.py
│       ├── tests.py
│       └── views.py
├── <project_name>
│   ├── __init__.py
│   ├── asgi.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── .gitignore
├── manage.py
└── config.json[可选]

使用教程

更多详细信息请参考在线文档

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

it-drf-utils-0.2.1.tar.gz (10.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

it_drf_utils-0.2.1-py3-none-any.whl (12.7 kB view details)

Uploaded Python 3

File details

Details for the file it-drf-utils-0.2.1.tar.gz.

File metadata

  • Download URL: it-drf-utils-0.2.1.tar.gz
  • Upload date:
  • Size: 10.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.13

File hashes

Hashes for it-drf-utils-0.2.1.tar.gz
Algorithm Hash digest
SHA256 246ccea8a1920869040d1d75c13f4bd1a09172c0be136cd3182a201cadfa8fc3
MD5 35fcdba9f4ac4533e8d85648aab7165b
BLAKE2b-256 b64d8f4226f5757fe9120ba2532b9851cbe6fcb6fdb8b6e570ff8a67c01a68db

See more details on using hashes here.

File details

Details for the file it_drf_utils-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: it_drf_utils-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 12.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.13

File hashes

Hashes for it_drf_utils-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 befcfa9dbfabdb525e122c9626343093be5c2feb1f31b7a0996f5101ea20304e
MD5 b9a2fcd80079ea4f3b11a6d8ff25e6f8
BLAKE2b-256 77926598301a0e53845541577e5ed70ceebe539833adecb5fa18d58cfe56dd03

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page