《轻量级Django》笔记

一个最小的Django项目文件,包含 views,Url模式和配置即可。

文件 hello.py

import sys

from django.conf import settings
from django.conf.urls import url
from django.http import HttpResponse


# views
def index(request):
    return HttpResponse('Hello World')


# URL patterns
urlpatterns = (
    url(r'^$', index),
)


# settings
settings.configure(
    DEBUG=True,
    SECRET_KEY='SECRET_KEY_HERE',
    ROOT_URLCONF=__name__,
    MIDDLEWARE_CLASSES=(
        'django.middleware.common.CommonMiddleware',
        # 'django.middleware.csrf.CsrfViewMiddleware',
        # 'django.middleware.clickjacking.XFrameOptionsMiddleware',
    ),
)


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

测试运行

在命令行中启动:python hello.py runserver
运行的结果:

>python hello.py runserver
Performing system checks...

System check identified no issues (0 silenced).
August 28, 2021 - 12:40:39
Django version 1.11.29, using settings None
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.

运行环境软件版本

Python version : 2.7.18
Django version : 1.11.29

标签: Python, Django

添加新评论