Python3学习笔记 初试Django框架Web项目
1. 开发环境
已经安装Python版本
d:\Python.prj>python -V
Python 3.8.7
已经安装Django版本
d:\Python.prj>python
Python 3.8.7 (tags/v3.8.7:6503f05, Dec 21 2020, 17:59:51) [MSC v.1928 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> django.VERSION
(3, 2, 0, 'alpha', 1)
>>>
2. 创建Web项目
django-admin startproject 项目名 [项目想要存放的路径/省略为当前路径]
d:\Python.prj\django-admin startproject first_web
d:\Python.prj>dir
驱动器 D 中的卷没有标签。
卷的序列号是 3CAB-1FA7
d:\Python.prj 的目录
2021-02-18 16:29 <DIR> .
2021-02-18 16:29 <DIR> ..
2021-02-18 16:29 <DIR> first_web
0 个文件 0 字节
1 个目录 108,127,354,880 可用字节
d:\Python.prj>cd first_web
d:\Python.prj\first_web>dir/s
驱动器 D 中的卷没有标签。
卷的序列号是 3CAB-1FA7
d:\Python.prj\first_web 的目录
2021-02-18 16:29 <DIR> .
2021-02-18 16:29 <DIR> ..
2021-02-18 16:29 <DIR> first_web
2021-02-18 16:29 687 manage.py
1 个文件 687 字节
d:\Python.prj\first_web\first_web 的目录
2021-02-18 16:29 <DIR> .
2021-02-18 16:29 <DIR> ..
2021-02-18 16:29 411 asgi.py
2021-02-18 16:29 3,373 settings.py
2021-02-18 16:29 772 urls.py
2021-02-18 16:29 411 wsgi.py
2021-02-18 16:29 0 __init__.py
5 个文件 4,967 字节
所列文件总数:
6 个文件 5,654 字节
5 个目录 108,127,358,976 可用字节
查询项目版本
d:\Python.prj\first_web>python manage.py version
3.2a1
3. 项目结构
first_web
|--- first_web
|--- __init__.py // Python包标识文件
|--- asgi.py // ASGI协议文件
|--- settings.py // Web项目配置
|--- urls.py // URL路由配置
|--- wsgi.py // WSGI协议文件
|--- manage.py // Django命令行工具
4. 启动Web服务
d:\Python.prj\first_web>python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (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.
February 18, 2021 - 16:12:18
Django version 3.2a1, using settings 'first_web.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
浏览器访问http://127.0.0.1:800/
Web 项目启动成功!
5. 创建html模板
项目根目录下创建templates目录,创建hello1.html和hello2.html模板文件
first_web
|--- templates
|--- hello1.html
|--- hello2.html
hello1.html
<title>第一天 hello1</title>
<h1>{{ hello }}</h1>
<p>{{var}}</p>
<a href="2/">第二天</a>
hello2.html
<title>第二天 hello2</title>
<h1>{{ hello }}</h1>
<p>{{var}}</p>
<a href="/">第一天</a>
两个模板文件中分别定义{{ hello }}和{{ var }}变量,动态改动相应内容。
6. 视图文件配置
first_web
|--- first_web
|--- views.py
创建views.py文件,定义视图文件及变量内容
from django.shortcuts import render
import time # 引入time模块
def hello1(request):
context = {}
context['hello'] = '我是高手!'
context['var'] = 2323+1
return render(request, 'hello1.html', context)
def hello2(request):
context = {}
context['hello'] = '我怕谁!'
localtime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
context['var'] = localtime
return render(request, 'hello2.html', context)
7. 模板路径设置
编辑settings.py文件
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
指定模板文件路径
'DIRS': [os.path.join(BASE_DIR, 'templates')],
settings.py文件头部引入os
import os
不然路径代码’DIRS’: [os.path.join(BASE_DIR, ‘templates’)],会报错
File "d:\Python.prj\first_web\first_web\settings.py", line 57, in <module>
'DIRS': [os.path.join(BASE_DIR, 'templates')],
NameError: name 'os' is not defined
8. URL页面路由配置
编辑urls.py文件
from django.urls import path
"""导入视图文件"""
from . import views
"""设置页面路由"""
urlpatterns = [
path('', views.hello1),
path('2/', views.hello2),
]
9. 重新启动Web服务
d:\Python.prj\first_web>python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (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.
February 18, 2021 - 16:12:18
Django version 3.2a1, using settings 'first_web.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
浏览器访问http://127.0.0.1:8000/
显示hello1.html内容
访问http://127.0.0.1:8000/2/
显示hello2.html内容
到此,顺利完成Django框架搭建Web项目的基本流程。
本章完