Part 1
查询ubuntu 16.04机器安装的Django版本为Django 2.0版本。Django 2.0版本支持Python 3.4及其后续版本。
root@imhqq-Lenovo-H5050:/home/imhqq/share/django_dev/mysite# python -m django --version
2.0
What Python version can I use with Django?¶
Django version | Python versions |
---|---|
1.8 | 2.7, 3.2 (until the end of 2016), 3.3, 3.4, 3.5 |
1.9, 1.10 | 2.7, 3.4, 3.5 |
1.11 | 2.7, 3.4, 3.5, 3.6 |
2.0 | 3.4, 3.5, 3.6 |
2.1 | 3.5, 3.6, 3.7 |
新建一个mysite django项目:
root@imhqq-Lenovo-H5050:/home/imhqq/share/django_dev# django-admin.py startproject mysite
root@imhqq-Lenovo-H5050:/home/imhqq/share/django_dev# ls
helloworld mysite
root@imhqq-Lenovo-H5050:/home/imhqq/share/django_dev# cd mysite/
root@imhqq-Lenovo-H5050:/home/imhqq/share/django_dev/mysite# tree
.
├── manage.py
└── mysite
├── init.py
├── settings.py
├── urls.py
└── wsgi.py
1 directory, 5 files
文件说明:
manage.py:命令行工具,用于与Django项目以不同方式进行交互。
mysite:项目容器。
init.py:空文件,告诉Python这是一个Python包。
settings.py: Django项目的设置与配置。
urls.py:该Django项目的URL声明。
wsgi.py:服务于这个项目的WSGI兼容的web服务器入口点。
开发服务器:
root@imhqq-Lenovo-H5050:/home/imhqq/share/django_dev#cd mysite/
root@imhqq-Lenovo-H5050:/home/imhqq/share/django_dev/mysite# python manage.py runserver
Performing system checks...
System check identified no issues (0 silenced).
You have 14 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.
December 30, 2017 - 00:41:45
Django version 2.0, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
[30/Dec/2017 00:41:52] "GET / HTTP/1.1" 200 16559
[30/Dec/2017 00:41:52] "GET /static/admin/css/fonts.css HTTP/1.1" 304 0
[30/Dec/2017 00:41:52] "GET /static/admin/fonts/Roboto-Regular-webfont.woff HTTP/1.1" 304 0
[30/Dec/2017 00:41:52] "GET /static/admin/fonts/Roboto-Bold-webfont.woff HTTP/1.1" 304 0
[30/Dec/2017 00:41:52] "GET /static/admin/fonts/Roboto-Light-webfont.woff HTTP/1.1" 304 0
这表示已经开启了一个Django开发服务器。runserver是一个纯Python写的轻量级服务器。
web浏览器访问http://127.0.0.1:8000/试试,默认端口号是8000.
改变网络端口:
root@imhqq-Lenovo-H5050:/home/imhqq/share/django_dev/mysite# python manage.py runserver 8080
Performing system checks...
System check identified no issues (0 silenced).
You have 14 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.
December 30, 2017 - 00:45:30
Django version 2.0, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8080/
Quit the server with CONTROL-C.
web浏览器访问http://127.0.0.1:8080/试试,可以看到一个小火箭。
产生一个Polls App:
root@imhqq-Lenovo-H5050:/home/imhqq/share/django_dev/mysite# pwd
/home/imhqq/share/django_dev/mysite
root@imhqq-Lenovo-H5050:/home/imhqq/share/django_dev/mysite# python manage.py startapp polls
root@imhqq-Lenovo-H5050:/home/imhqq/share/django_dev/mysite#
root@imhqq-Lenovo-H5050:/home/imhqq/share/django_dev/mysite# tree
.
├── db.sqlite3
├── manage.py
├── mysite
│ ├── init.py
│ ├── pycache
│ │ ├── init.cpython-35.pyc
│ │ ├── settings.cpython-35.pyc
│ │ ├── urls.cpython-35.pyc
│ │ └── wsgi.cpython-35.pyc
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
└── polls
├── admin.py
├── apps.py
├── init.py
├── migrations
│ └── init.py
├── models.py
├── tests.py
└── views.py
4 directories, 17 files
写第一个view:
polls/views.py
from django.shortcuts import render
Create your views here.
from django.http import HttpResponse
def index(request):
return HttpResponse("hello, world. You're at the polls index.")
要调用这个视图,需要将这个视图映射到URL上,所以还需要一个URLconf。
polls/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('',views.index,name='index'),
]
接下来,在polls.urls模块中,指定root URLconf(根URLconf)。在mysite/urls.py中增加引入django.urls.include,并在urlpatterns列表中插入include().
mysite/urls.py
"""mysite URL Configuration
The urlpatterns
list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/2.0/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import include,path
urlpatterns = [
path('polls/', include('polls.urls'),
path('admin/', admin.site.urls),
]
include()函数允许索引其他URLconfs。
现在,已经将一个index视图(view)链接到URLconf。
root@imhqq-Lenovo-H5050:/home/imhqq/share/django_dev/mysite# python manage.py runserver
Performing system checks...
System check identified no issues (0 silenced).
December 30, 2017 - 02:32:29
Django version 2.0, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
web浏览器输入http://127.0.0.1:8000/polls,会显示“hello world.You're at the polls index.”
path()函数传递4个参数,2个必选:route和view,2个可选:kwargs和name。
path()参数 介绍:
route参数
route是一个包含URL式样的字符串。当处理一个请求,Django从urlpatterns中的第一个pattern(式样)开始,依次遍历列表,与每个pattern(式样)比较,直到找到匹配的pattern(式样)。
view参数
当Django找到匹配的模式,它调用特定的带HttpRequest对象的view函数,作为其第一个参数,并从route捕获值作为关键参数。
kwargs参数
任意关键字参数可以字典方式传入目标视图。
name参数
命名你的URL,让你在Django的任何地方都清晰地引用URL,特别是在模板中。