Django的构建配方
djangorecipe的Python项目详细描述
djangoRecipe:使用buildout轻松安装django
使用djangoRecipe,您可以以熟悉的方式管理django站点 构建用户。例如:
- bin/django 运行django而不是 bin/python manage.py
- bin/test 运行测试,而不是 bin/python manage.py测试项目。 (包括围绕您的测试运行覆盖范围)。
- bin/django 自动使用正确的django设置。所以你可以 a development.cfg 构建配置和a production.cfg DjangoRecipe以使用不同的Django设置模块。 bin/django 将 自动使用正确的设置,无需设置环境变量。
DjangoRecipe是在Github上开发的 https://github.com/rvanlaar/djangoRecipe ,您可以在那里提交错误报告。它 使用travis ci进行测试,并通过landscape检查代码质量。io:
设置
你可以看到一个如何使用下面的食谱的例子 常用设置:
[buildout] show-picked-versions = true parts = django eggs = yourproject gunicorn develop = . # ^^^ Assumption: the current directory is where you develop 'yourproject'. versions = versions [versions] Django = 1.8.2 gunicorn = 19.3.0 [django] recipe = djangorecipe settings = development eggs = ${buildout:eggs} project = yourproject test = yourproject scripts-with-settings = gunicorn # ^^^ This line generates a bin/gunicorn-with-settings script with # the correct django environment settings variable already set.
DjangoRecipe的早期版本用于为您创建项目结构, 如果你想的话。django本身现在产生了良好的项目结构。只是 运行 bin/django startproject<;projectname>; 。创建的主目录是 你应该把你的构建和可能的一个 setup.py
startproject为您创建一个 manage.py 脚本。你可以移除它,因为 bin/django djangoRecipe创建的脚本是(几乎完全正确的) 更换。
请参阅django的文档以了解 startproject
您还可以查看cookiecutter
支持的选项
配方支持以下选项。
< dl >添加到此处的脚本名称(如"gunicorn")获取重复的脚本 创建时在其后面加上"-with settings"(因此: bin/gunicorn和设置。他们得到设置环境变量 集合。目前,它主要用于不能运行的Gunicorn 在Django进程中。所以脚本必须已经通过 正确的设置环境变量。
注意 :将脚本打包一定是在你的"鸡蛋"选项里 部分。因此,如果使用gunicorn,请将其添加到此处(或将其添加为 您的项目)。
以下选项主要适用于旧项目或特殊情况:
< dl >覆盖说明
从django 1.7开始,不能使用自定义测试运行程序(如django nose) 在启用覆盖率的情况下自动运行测试。新的应用程序 初始化机制已经加载了 models.py ,例如, 在调用测试运行程序之前。所以你的模型在很大程度上 未经测试。
当 coverage=true , bin/test 在django之前开始覆盖率记录 接到电话。它还打印出一个报告并导出xml结果(用于记录 例如,jenkins中的测试结果)和html结果。
在幕后, true 被转换为默认的 报表xml_report HTML U报告 。这些以空格分隔的函数名依次调用 覆盖率实例。有关可用的 功能。如果只需要快速报告和XML输出,可以设置 覆盖率=报告XML\u报告 而不是。
注意,不能将选项传递给这些函数,如html输出 位置。为此,在 buildout.cfg>旁边添加一个 .coveragerc 。见 覆盖率配置文件文档 。下面是一个示例:
[run] omit = */migrations/* *settings.py source = your_app [report] show_missing = true [html] directory = htmlcov [xml] output = coverage.xml
mod_wsgi的配置示例
如果要使用mod_wsgi部署项目,可以使用 示例作为起点:
<Directory /path/to/buildout> Order deny,allow Allow from all </Directory> <VirtualHost 1.2.3.4:80> ServerName my.rocking.server CustomLog /var/log/apache2/my.rocking.server/access.log combined ErrorLog /var/log/apache2/my.rocking.server/error.log WSGIScriptAlias / /path/to/buildout/bin/django.wsgi </VirtualHost>
角落案例:当几个wsgi脚本组合在一个 apache的单个虚拟主机实例。这是因为Django 使用环境变量django_settings_module。设置此变量 当加载第一个wsgi脚本时。其余的wsgi脚本将失败, 因为他们需要不同的设置模块。但是环境 变量django_settings_module只设置一次。新的初始化 添加到djangoRecipe的选项可用于解决此问题 如下所示:
[django] settings = acceptance initialization = import os os.environ['DJANGO_SETTINGS_MODULE'] = '${django:project}.${django:settings}'
为pydev生成控制脚本
在pydev中使用auto reload运行django需要添加一个小片段 代码:
import pydevd pydevd.patch_django_autoreload(patch_remote_debugger=False, patch_show_console=True)
就在 manage.py 模块(或 在这种情况下,通常是生成的控制脚本。这个 下面的buildout示例生成两个控制脚本:一个用于命令行 用法和pydev的用法,以及所需的片段,使用配方的 初始化 选项:
[buildout] parts = django pydev eggs = mock [django] recipe = djangorecipe eggs = ${buildout:eggs} project = dummyshop [pydev] <= django initialization = import pydevd pydevd.patch_django_autoreload(patch_remote_debugger=False, patch_show_console=True)
django配置的示例用法
django配置(http://django-configurations.readthedocs.org/en/latest/" rel="nofollow">http://django-configurations.readthedocs.org/en/latest/) 是一个帮助您将django设置组织到类中的应用程序。 使用它需要修改manage.py文件。使用 配方的初始化选项:
[buildout] parts = django eggs = hashlib [django] recipe = djangorecipe eggs = ${buildout:eggs} project = myproject initialization = # Patch the manage file for django-configurations import os os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings') os.environ.setdefault('DJANGO_CONFIGURATION', 'Development') from configurations.management import execute_from_command_line import django django.core.management.execute_from_command_line = execute_from_command_line