注意:开发用的是Django 1.8.4,对照2.0文档,因此和老版本代码略微不同
Django官网: https://www.djangoproject.com
这个就是官网Wrting your first Django app的手册,想要提高自己阅读文档能力
的人去看看
https://docs.djangoproject.com/en/2.0/intro/tutorial01/
建立虚拟环境
为项目创建一个目录learning_log,在终端切换到这个目录创建一个虚拟环境
python -m venv 11_env
激活虚拟环境
linux中:
learning_log$ source 11_env/bin/activate
(11_env)learning_log$
windows中:
执行批处理文件
进入虚拟环境D:\learning_log>call 11_env\Scripts\activate.bat
退出虚拟环境D:\learning_log>call 11_env\Scripts\activate.bat
how to get Django
Option1: Get the latest official version
pip install Django==1.8.4
Option2: Get the latest development version
git clone https://github.com/django/django.git
也就是说直接pip安装或者用git克隆安装
安装Django 注意Django仅在虚拟环境处于活动状态时才可用
(11_env) D:\learning_log>pip install Django
(11_env) D:\learning_log>python -m django --version
1.8.4
在Django中创建项目
Creating a project
在命令行下,使用cd进入一个你喜欢的目录创建项目,然后使用下面的命令
From the command line, cd into a directory where you’d like to store
your code, then run the following command:
$ django-admin startproject mysite
这就是你的项目结构了
mysite/
manage.py
mysite/
__init__.py
settings.py
urls.py
wsgi.py
These files are:
外面的mysite根目录仅仅是你项目的容器,它的名字无关紧要。你随便起名字就行
The outer mysite/ root directory is just a container for your project.
Its name doesn’t matter to Django; you can rename it to anything you like.
命令行工具让你以多种方式和Django项目交互。其实也就是使用命令来管理诸如使用数据库
和运行服务器等任务
manage.py: A command-line utility that lets you interact with this Django
project in various ways. You can read all the details about manage.py in
django-admin and manage.py.内部的mysql目录也就是Python包名,你可以importmysql
目录中所有的东西
The inner mysite/ directory is the actual Python package for your project
. Its name is the Python package name you’ll need to use to import anything
inside it (e.g. mysite.urls).
mysite/__init__.py: An empty file that tells Python that this directory should
be considered a Python package. If you’re a Python beginner, read more about
packages in the official Python docs.
为Django设置配置。
mysite/settings.py: Settings/configuration for this Django project. Django
settings will tell you all about how settings work.
urls.py告诉Django应创建那些网页来相应浏览器请求
mysite/urls.py: The URL declarations for this Django project; a “table of
contents” of your Django-powered site. You can read more about URLs in URL dispatcher.
wsgi(web server gateway interfaceWeb服务器网关接口)帮助Django提供它创建的文件
mysite/wsgi.py: An entry-point for WSGI-compatible web servers to serve your
project. See How to deploy with WSGI for more details.