使用Django模板
1,
D:dj_examples>django-admin.py startproject muban
2
D:dj_examples>dir
3
2020/04/21 10:04 <DIR> muban
4
D:dj_examples>cd muban
5
dir
6
2020/04/21 10:04 646 manage.py
2020/04/21 10:04 <DIR> muban
7
D:dj_examplesmuban>cd muban
8
dir
9
2020/04/21 10:04 403 asgi.py
2020/04/21 10:04 3,205 settings.py
2020/04/21 10:04 768 urls.py
2020/04/21 10:04 403 wsgi.py
2020/04/21 10:04 0 __init__.py
10
cd..
11
D:dj_examplesmuban>manage.py runserver
12
D:dj_examplesmuban>manage.py runserver
Watching for file changes with StatReloader 监视文件的改变
Performing system checks... 系统检测
System check identified no issues (0 silenced).
You have 17 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
你有17个没有应用的迁移,你的项目可能不会工作,直到你应用这些迁移。
管理,认证,内容类型,会话。
Run 'python manage.py migrate' to apply them.
运行 python manage.py migrate 来应用他们
April 21, 2020 - 10:07:25
Django version 3.0.5, using settings 'muban.settings'
日期,时间
Django版本是3.0.5
使用的配置是muban.settings
Starting development server at http://127.0.0.1:8000/
启动开发服务器,地址url是127.....
Quit the server with CTRL-BREAK.
退出服务器,ctrl break
_
光标闪烁
[21/Apr/2020 10:13:56] "GET / HTTP/1.1" 200 16351
[21/Apr/2020 10:13:56] "GET /static/admin/css/fonts.css HTTP/1.1" 304 0
[21/Apr/2020 10:13:56] "GET /static/admin/fonts/Roboto-Regular-webfont.woff HTTP/1.1" 304 0
[21/Apr/2020 10:13:56] "GET /static/admin/fonts/Roboto-Bold-webfont.woff HTTP/1.1" 304 0
[21/Apr/2020 10:13:56] "GET /static/admin/fonts/Roboto-Light-webfont.woff HTTP/1.1" 304 0
新建一个模板
在哪里新建?
在
D:dj_examplesmuban 的目录
2020/04/21 10:21 <DIR> .
2020/04/21 10:21 <DIR> ..
2020/04/21 10:07 0 db.sqlite3
2020/04/21 10:04 646 manage.py
2020/04/21 10:07 <DIR> muban
2020/04/21 10:22 <DIR> templates
D:dj_examplesmubantemplates 的目录
2020/04/21 10:22 <DIR> .
2020/04/21 10:22 <DIR> ..
2020/04/21 10:22 18 hello.html
hello.html的内容
<h1>{{hello}}</h1>
打开配置文件
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [BASE_DIR+'/templates'], #修改路径
新建view.py
在哪里新建
D:dj_examplesmubanmuban 的目录
2020/04/21 10:30 <DIR> .
2020/04/21 10:30 <DIR> ..
2020/04/21 10:04 403 asgi.py
2020/04/21 10:26 3,240 settings.py
2020/04/21 10:04 768 urls.py
2020/04/21 10:29 160 view.py
from django.shortcuts import render
def hello(request):
context={}
context['hello']='hello world hi'
return render(request,'hello.html',context)
修改urls.py
调用view.py
#在里面调用view.py定义的函数
from django.urls import path
from . import view
urlpatterns=[
path('hello/',view.hello)#调用了hello
]
url里面调用了view
view里面传入了hello.html
settings指明了helo.html所在路径
render()根据
hello.html
查询到了hello变量
hello变量,根据context
查询到了内容
所以,hello.html里面存储的是字典的key
网页看到的内容是字典根据key查询返回的内容
启动服务器
D:dj_examplesmuban>manage.py runserver
总结: