惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

C
Cisco Blogs
The Cloudflare Blog
云风的 BLOG
云风的 BLOG
Recorded Future
Recorded Future
F
Fortinet All Blogs
Microsoft Azure Blog
Microsoft Azure Blog
U
Unit 42
博客园 - 三生石上(FineUI控件)
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
N
Netflix TechBlog - Medium
C
Check Point Blog
Security Latest
Security Latest
MongoDB | Blog
MongoDB | Blog
P
Proofpoint News Feed
J
Java Code Geeks
The GitHub Blog
The GitHub Blog
V
Vulnerabilities – Threatpost
阮一峰的网络日志
阮一峰的网络日志
P
Palo Alto Networks Blog
C
CERT Recently Published Vulnerability Notes
F
Full Disclosure
C
Cyber Attacks, Cyber Crime and Cyber Security
雷峰网
雷峰网
T
The Blog of Author Tim Ferriss
T
Threat Research - Cisco Blogs
Cisco Talos Blog
Cisco Talos Blog
V
V2EX
Latest news
Latest news
Engineering at Meta
Engineering at Meta
A
About on SuperTechFans
Cyberwarzone
Cyberwarzone
The Hacker News
The Hacker News
A
Arctic Wolf
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
NISL@THU
NISL@THU
腾讯CDC
P
Privacy & Cybersecurity Law Blog
G
GRAHAM CLULEY
罗磊的独立博客
S
Schneier on Security
C
Cybersecurity and Infrastructure Security Agency CISA
博客园 - 司徒正美
K
Kaspersky official blog
L
Lohrmann on Cybersecurity
C
CXSECURITY Database RSS Feed - CXSecurity.com
月光博客
月光博客
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Project Zero
Project Zero
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
I
InfoQ

夜行人

回家路上 第一期的直播演示项目 震动检测器 正能量 在线参观CodeLab Neverland 发布 CodeLab Adapter 3.3.1 DynamicTable 之 纸糊方向盘 CodeLab DynamicTable: 一个可实施的技术方案 CodeLab Insight 发布 Alpha 版 情人节 Home Assistant 周报 && IoT 周报 (02) Joplin: 关注隐私的 Evernote 开源替代软件 浏览器的未来与 Web 传感器 Home Assistant 周报 && IoT 周报 (01) 百宝箱(01) 论自由 介绍 WebThings Home Assistant 周报 && iot 周报 (00) 百宝箱(00) 毛姆读书心得 传世之作 周末徒步 CodeLab Adapter ❤️ Jupyter/Python 航班 躲雨 夏令营途中 [译]思想--作为一种技术 The future of coding 美国之行 三门问题的程序模拟 从Python转向Pharo https://blog.just4fun.site/post/iot/iot-open-source-projects/ Python异步编程笔记 https://blog.just4fun.site/post/iot/iot-open-source-hardware-community/ 万物积木化开发者社区 CodeLab ❤️ Blender Scratch3技术分析之云变量 API(第7篇) [译]对管道(Pipes)的偏爱 [译]提出正确的问题比得到正确答案更重要 蓝牙设备与Scratch3.0 创建你的第一个Scratch3.0 Extension Scratch3技术分析之项目内部数据(第6篇) Scratch3技术分析之社区 API(第5篇) Scratch3技术分析之User API(第4篇) Scratch3技术分析之项目主页API(第3篇) Scratch3技术分析之静态资源API(第2篇) Scratch3.0、micro:bit与Windows7 https://blog.just4fun.site/post/iot/zerynth-vs-micropython/ 核聚变、方所与半宅空间 可视化编程为何是个糟糕的主意 codelab.club周末聚会 关于codelab.club '下一件大事'是一个房间 Hungry Robot - Eat everything 编程作为一种思考方式 今日简史 史蒂夫·乔布斯传 罗素自选文集 https://blog.just4fun.site/post/edx/tianjin-scratch-ai/ https://blog.just4fun.site/post/edx/richie-cms-openedx/ 徒步武功山 WebUSB与micro:bit 积木化编程与3D场景 夜宿武功山顶 scratch3-adapter接入优必选Alpha系列机器人 https://blog.just4fun.site/post/edx/video-migration-note/ scratch3-adapter重构笔记 https://blog.just4fun.site/post/edx/edx-community-members/ 两种硬件编程风格的比较 使用micro:bit自制PPT翻页笔 柏拉图对话集 scratch3.0 + micro:bit 七月电影放映计划 非营利组织的管理 Screenly--用树莓派让任何屏幕变为可编程的数字标牌 micro:bit与事件驱动 为Scratch3.0设计的插件系统(上篇) OCR应用一例 近两年读过的一些好书 blockly开发之使用python驱动浏览器中的turtle(2) 牛顿新传 文学理论入门 逻辑的引擎 人生的意义 blockly开发之生成并运行js代码(1) blockly开发之hello world(0) micro:bit使用笔记 神器之Termux https://blog.just4fun.site/post/iot/micropython-notes/ Cozmo what is this Scratch的前世今生 下段旅程 我行在远方 爆裂 途中杂记 https://blog.just4fun.site/post/edx/open-edx-startup/ cozmo系列之入门 - 有性格且可编程的机器人 PaperWeekly开发笔记 创业二三事 blockly与结对编程
以最佳实践开始你的Django项目
2018-06-21 · via 夜行人

文章目录

“How long will it take to fix that?” One person, who’d been at the company for years, said, “Three months.” A new person, who’d just come from a world of rapidly provisioned cloud microservices, said, “Three minutes.” They were both correct. That’s how change enters into this world – what is code

前言

我近年写过的Django项目基本都采用cookiecutter-django来启动

Cookiecutter Django is a framework for jumpstarting production-ready Django projects quickly.

cookiecutter-django将帮助你快速构建一个生产可用的项目骨架,通过初始化项目时回答几个问题,你将开箱得到许多常用的东西(往往遵循最佳实践),而避免配置

cookiecutter-djangopydanny构建,pydanny写过一本Django最佳实践相关的书《Two Scoops of Django》

cookiecutter-django介绍

cookiecutter-django相关的信息,项目readme里写的很清楚,我简单做些脚注

Features

Documentation

我倾向于使用docker,文档在Getting Up and Running Locally With Docker

建议有空多翻阅文档,文档里把开发和部署的大多数问题都谈论了

一些docker命令偏长,可以自行写个makefile

服务器的远程管理,推荐使用fabricansible比较重

optional-integrations

  • Serve static files from Amazon S3 or Whitenoise
    • 我偏向把文件存于本地,之后使用七牛云做镜像到本地,来分发(CDN),使用Whitenoise就不必使用nginx来处理静态文件了
    • 关于文件管理可以看下Caddy的插件filebrowser
  • Configuration for Celery
  • Integration with MailHog for local email testing
  • Integration with Sentry for error logging
    • 错误提醒及时(邮件通知),错误信息详细
    • 可以直接使用官方的云平台,不必自己搭建,目前国内可以正常使用

使用笔记

创建项目

我们使用cookiecutter替代django-admin startproject来初始化项目.

首先安装依赖pip3 install "cookiecutter>=1.4.0"

接着开始创建项目cookiecutter https://github.com/pydanny/cookiecutter-django,你讲被要求回答几个问题,关于项目的信息,以及你需要的组件,我的回答见: start_django_project.md

运行项目

我使用docker来运行项目,我们需要安装:

Build the Stack

docker-compose -f local.yml build

生产环境使用production.yml替代local.yml

第一次build需要一断时间

如果你添加了python库,需要重新build,不过所花时间要少很多

Run the Stack

docker-compose -f local.yml up

或者

1
2
3
export COMPOSE_FILE=local.yml
docker-compose up 
# docker-compose up -d # 后台运行

Execute Management Commands

1
2
docker-compose -f local.yml run --rm django python manage.py migrate
docker-compose -f local.yml run --rm django python manage.py createsuperuser # django django123

以上每个进程都跑在一个单独的容器中(无状态),进程结束即销毁,数据库使用公共的(由docker-compose up之前启动的)

其他

  • Mailhog跑在http://127.0.0.1:8025
  • 数据备份
    • 备份: docker-compose -f local.yml exec postgres backup
    • 列出: docker-compose -f local.yml exec postgres backups
    • 复制到本地: docker cp 9c5c3f055843:/backups/backup_2018_03_13T09_05_07.sql.gz .
    • 从本地恢复: docker-compose -f local.yml exec postgres restore backup_2018_03_13T09_05_07.sql.gz

一些个人意见

下边是一些围绕构建一个django项目,我个人所采取的一些策略

用户系统

使用django-allauth

用户账号相关的页面由后端做(django-allauth基本都做了,这部分逻辑考虑步骤可能会有安全问题),此外部分前后端分离

如果希望注册和登陆更灵活(提供REST接口),可以使用django-rest-auth。django-rest-auth做的主要工作是让django-allauth暴露出API。

关于User model

如果你对django自带的User model感到满意, 只是想额外增加field的话, 你可以扩展AbstractUser类,pydanny在《Two Scoops of Django》讨论过这块,提到有几种策略。扩展AbstractUser是第一种,目前cookiecutter-django用的也是这种

添加django-rest-auth

添加依赖之后,记得docker-compose -f local.yml build

django-rest-auth暴露出以下API

  • http://127.0.0.1:8000/rest-auth/user/ 将允许你在web页面调试获取用户数据和注册用户
  • http://127.0.0.1:8000/rest-auth/registration/ 中注册成功之后,验证邮箱会出错,解决方案参考ImproperlyConfigured at /rest-auth/registration/account-confirm-email,直接使用allauth提供的confirm_email即可
  • 登陆(换取token): http POST http://127.0.0.1:8000/rest-auth/login/ username=django password=django123

github登陆

记录一下,配置github登陆的流程

  • 添加INSTALLED_APPS : Installation
  • 到github注册client: providers github
  • 在admin中配置github信息: http://127.0.0.1:8000/admin/socialaccount/socialapp/

oauth github

开发者协同

  • git pull
  • docker build
  • 数据库分发备份

表单

  • 采集数据的表单,可以由后端来做,然后放在django admin里管理
    • admin + 表单十分迅速

参考

文章作者 种瓜

上次更新 2018-06-21