what is Django?
Django is a advancedPython Webframework, 它鼓励 fast 速Development and 简洁Practical design. Django遵循MVC (model-视graph-控制器) architecture模式, providing了强 big datalibrary访问API, 自动生成 management界面, URLrouting, 模板systemetc.functions, 使Development者able to fast 速构建 high quality Webapplication.
提示
Django design理念 is "不要重复自己" (DRYprinciples) , 它providing了许 many in 置functions, 避免Development者writing重复code.
Django 主要特点
- fast 速Development: Djangoproviding了丰富 in 置functions, 使Development者able to fast 速构建Webapplication.
- security: Django in 置了 many 种security mechanisms, such as防止SQL注入, 跨站脚本攻击, 跨站request伪造etc..
- 可scale: Django module化design使其易于scale, 可以throughinstallation第三方application来增加functions.
- 强 big management界面: Django自动生成management界面, 方便Development者managementapplicationdata.
- flexible URLrouting: Django URLroutingsystem允许Development者design简洁, 易读 URL.
- 模板system: Django 模板systemsupportvariable, filter器, tagetc., 方便Development者生成HTML页面.
- datalibrarysupport: Djangosupport many 种datalibrary, such asPostgreSQL, MySQL, SQLiteetc..
installation Django
in installationDjango之 before , 你需要先installationPython. Django 3.2及以 on version需要Python 3.6 or 更 high version.
usingpipinstallationDjango
可以usingPython packagemanagementtoolpip来installationDjango:
pip install django
installationspecificversion Django:
pip install django==4.2
verificationinstallation
installationcompletion after , 可以using以 under commandsverificationDjango is 否installation成功:
django-admin --version
such as果installation成功, 会显示Django version号, 例such as:
4.2.7
creationDjangoproject
usingdjango-admincommandscreation一个 new Djangoproject:
django-admin startproject mysite
这会 in 当 before Table of Contents under creation一个名 for mysite Table of Contents, 其inpackage含以 under file and Table of Contents:
mysite/
├── manage.py
└── mysite/
├── __init__.py
├── asgi.py
├── settings.py
├── urls.py
└── wsgi.py
projectstructure说明
- manage.py: Djangoproject commands行tool, 用于managementproject.
- mysite/: project 主Table of Contents, package含project configurationfile.
- mysite/__init__.py: 一个空file, 告诉Python这 is a Pythonpackage.
- mysite/asgi.py: ASGI兼容 Webserver入口, 用于deploymentproject.
- mysite/settings.py: project configurationfile, package含datalibraryconfiguration, applicationconfiguration, 静态fileconfigurationetc..
- mysite/urls.py: project URLroutingconfigurationfile.
- mysite/wsgi.py: WSGI兼容 Webserver入口, 用于deploymentproject.
runDevelopmentserver
进入projectTable of Contents, usingmanage.pycommands启动Django Developmentserver:
cd mysite
python manage.py runserver
such as果server启动成功, 会显示以 under information:
Watching for file changes with StatReloader
Performing system checks...
System check identified no issuess (0 silenced).
You have 18 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
November 01, 2023 - 15:30:00
Django version 4.2.7, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
现 in 可以 in 浏览器in访问http://127.0.0.1:8000/, 看 to Django 欢迎页面.
warning
Developmentserver仅用于Developmentenvironment, 不要 in produceenvironmentinusing.
练习 1: installationDjango并creationproject
- installationPython 3.6 or 更 high version (such as果尚未installation) .
- usingpipinstallationDjango.
- verificationDjangoinstallation is 否成功.
- creation一个名 for "mydjango" Djangoproject.
- 启动Developmentserver并 in 浏览器in访问.