print
。Apache 有大量的配置需要去理解,这是我最受不了的。而 Gunicorn 非常简单,你只需要
gunicorn myproject.wsgi
当然,处理静态文件,我推荐 Nginxdocuments
,运行类似 MongoDB 那样的查询(带索引),而不会影响性能。最最重要的是,你仍然可以使用 Django ORM 和 Django admin
最近笔者看到一个统计报告,关系数据库的份额不降反增,啧啧reverse
来将别名转换为 URL
from django.urls import reverse from django.http import HttpResponseRedirect def redirect_to_year(request): # ... year = 2006 # ... return HttpResponseRedirect(reverse('news-year-archive', args=(year,)))
前端则使用 URL 的模板标签
<a href="{% url 'news-year-archive' 2012 %}">2012 Archive</a> <ul> {% for yearvar in year_list %} <li> <a href="{% url 'news-year-archive' yearvar %}">{{ yearvar }} Archive</a> </li> {% endfor %} </ul>
看起来蛮笨的,但是它会为您节省大量的时间,因为没有任何 URL 的硬编码,一个 URL 的改变,不会破坏任何的单元测试。common.py
保存,再创建两个配置文件:prod_settings.py
settings.py
它们均会在开头将 common.py
中的所有配置导入进去,再差异化的添加配置(同名也可以覆盖),像这样:
from .common import *
然后修改 wsgi.py
"""
WSGI config for helloworld project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/2.0/howto/deployment/wsgi/
"""
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.prod_settings") <--- 注意这里
application = get_wsgi_application()
这样,我可以直接在本地使用:
python manage.py runserver
现在你明白为什么我们仍然使用 settings.py 作为开发环境配置的名字的原因了吗?
而在生产环境,我们可以直接:
gunicorn myproject.wsgi
So easy!
后续会出一个全栈教程