极简 Django 项目工程

和一些后端的开发工程师交流的时,经常会听到这样的声音「你们用 Django 啊?太重了吧,用 Flask 就好」。今天无意中看到一篇将 Django 视作「微框架」的文章,发布的日期是 2011 年,虽然针对的是旧版本 Django,但是这么些 Django 本身的框架结构几乎没什么变化,除了当前的版本里会强制要求配置settings.ROOT_URLCONF 与文章中的说法不一致外,这篇文章的思路在今天依然是适用的。

# web.py
from django.conf import settings
from django.conf.urls import url
from django.http import HttpResponse


# basic view function
def index_view(request):
    response = HttpResponse('Hello, Django!')
    return response

# url routers
urlpatterns = [
        url(r'^$', index_view, name='index_view'),
        ]

# basic configurations
if not settings.configured:
    settings_kwargs = {
            'DEBUG': True,
            'ROOT_URLCONF': urlpatterns,
            }
    settings.configure(**settings_kwargs)

if __name__ == '__main__':
    from django.core.management import execute_from_command_line
    execute_from_command_line()

# 在 shell 中执行以下命令
python web.py runserver
Advertisements
此条目发表在Python分类目录,贴了标签。将固定链接加入收藏夹。

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s