路由系统 - urls.py
基本用法(完全匹配, 正则匹配, 别名, 子路由)
总 urls.pyfrom django. urls import path, re_path, include
from main_page import views as main_page_views
from host import urls as host_urls
urlpatterns = [
path( "index/" , main_page_views. index, name= "index" ) ,
re_path( r"^main_page/" , main_page_views. main_page) ,
re_path( r"^host/" , include( host_urls) ) ,
]
子 urls.pyfrom django. urls import path
from host import views
urlpatterns = [
path( "add/" , views. add) ,
]
views.pyfrom django. shortcuts import render, redirect, HttpResponse
from django. urls import reverse
def index ( request) :
return HttpResponse( "index" )
def main_page ( request) :
return redirect( reverse( "index" ) )
传参用法(位置参数, 关键字参数)
urls.py
from django. urls import re_path
urlpatterns = [
re_path( r"re1/(\d+)/(\d+)/$" , views. re_1) ,
re_path( r"^re2/(?P<num2>\d+)/(?P<num1>\d+)" , main_page_views. re_2) ,
]
views.py
def re_1 ( request, x, y) :
r = int ( x) + int ( y)
return HttpResponse( r)
def re_2 ( request, num1, num2) :
return HttpResponse( num1+ num2)
视图系统 - views.py
FBV 和 CBV
urls.pyurlpatterns = [
re_path( "^FBV$" , main_page_views. fbv) ,
re_path( "^CBV$" , main_page_views. CBV. as_view( ) ) ,
]
views.py
from django import views
def fbv ( request) :
return HttpResponse( "FBV" )
class CBV ( views. View) :
def get ( self, request) :
return HttpResponse( "CBV - get" )
def post ( self, request) :
return HttpResponse( "CBV - post" )
def put ( self, request) :
return HttpResponse( "CBV - put" )
def delete ( self, request) :
return HttpResponse( "CBV - del" )
给 CBV 加装饰器
from django. utils. decorators import method_decorator
def wrapper ( func) :
def inner ( * args, ** kwargs) :
ret = func( * args, ** kwargs)
return ret
return inner
class Asset ( views. View) :
def get ( self, request) :
pass
@method_decorator( wrapper)
def post ( self, request) :
pass
直接返回 json 格式 - JsonResponse 模块
views.py
from django. http import JsonResponse
def json1 ( request) :
r = { "a" : 1 , "b" : 2 }
return JsonResponse( r)
def json2 ( request) :
r = [ 1 , 2 , 3 , 4 ]
return JsonResponse( r, safe= False )
template – 模板系统
在 templates 目录下寻找 html 文件 查找顺序: 项目 templates 目录 --> app 内 templates 目录
变量语法示例 - {{ }}
views.py
def index ( request) :
name = "Tim"
score1 = { "math" : 99 , "chinese" : 98 }
score2 = [ "math" , "chinese" ]
english_score1 = ""
english_score2 = "默认值 - 不是空值"
filesize = 10240000
link = "<a href=http://www.baidu.com>baidu</a>"
return render( request, "test.html" , {
"name" : name,
"score1" : score1,
"score2" : score2,
"english_score1" : english_score1,
"english_score2" : english_score2,
"today" : today,
"filesize" : filesize,
"link" : link,
{ { today| date: "Y-m-d H:m:s" } }
"data" : locals ( ) }
)
test.html<!DOCTYPE html>
< html lang = " en" >
< head>
< meta charset = " UTF-8" >
< title> Title</ title>
</ head>
< body>
{{ name }}< br>
{{ score1.math }}< br>
{{ score2.0 }}< br>
{{ english_score1|default:"数据暂时为空" }}< br>
{{ english_score2|default:"数据暂时为空" }}< br>
{{ filesize|filesizeformat }}< br>
{{ today|date:"Y-m-d H:m:s" }}< br>
{{ link }}< br>
{{ link|safe }}< br>
</ body>
</ html>
结果
循环语法示例 - {% %}
views.pydef index ( request) :
l1 = [ 100 , 200 , 300 , "" , 500 ]
l2 = [ ]
return render( request, "test.html" , { "data" : locals ( ) } )
test.html<!DOCTYPE html>
< html lang = " en" >
< head>
< meta charset = " UTF-8" >
< title> Title</ title>
</ head>
< body>
l1 结果:< br>
{% for i in data.l1 %}
序号:
{{ forloop.counter }}
值:
{% if forloop.last %}
{{ i|default:"空值" }}
{% else %}
{{ i|default:"空值" }}< br>
{% endif %}
{% empty %}
"数据整个为空"
{% endfor %}
< br> ==========< br>
l2 结果:< br>
{% for i in data.l2 %}
序号:
{{ forloop.counter }}
值:
{% if forloop.last %}
{{ i|default:"空值" }}
{% else %}
{{ i|default:"空值" }}< br>
{% endif %}
{% empty %}
"数据整个为空"
{% endfor %}
</ body>
</ html>
图示
配合路由别名使用 - {% url %}
urls.pyfrom django. contrib import admin
from django. urls import path, re_path, include
from index import views as index_views
from host import urls as host_urls
urlpatterns = [
path( 'admin/' , admin. site. urls) ,
path( "login" , index_views. login, name= "denglu" ) ,
re_path( r"^login1/(\d+)/" , index_views. login1, name= 'denglu1' ) ,
re_path( r"^login2/(?P<pk>\d+)/" , index_views. login2, name= 'denglu2' ) ,
]
view.spydef login ( request) :
return render( request, "login.html" )
def login1 ( request, arg1) :
return render( request, "login1.html" )
def login2 ( request, pk) :
return render( request, "login2.html" )
html
{% url "denglu" data %}
{% url "denglu1" data %}
{% url "denglu2" data %}
母版和组件
母版文件<!DOCTYPE html>
< html lang = " en" >
< head>
< meta charset = " utf-8" >
< meta http-equiv = " X-UA-Compatible" content = " IE=edge" >
< meta name = " viewport" content = " width=device-width, initial-scale=1" >
< title>
{% block page_title %}
标题的位置
{% endblock %}
</ title>
< link href = " /static/bootstrap-3.3.7-dist/css/bootstrap.min.css" rel = " stylesheet" >
{% block page_css %}
单页面引入 css 文件位置
{% endblock %}
</ head>
< body>
< div class = " row" >
< div>
</ div>
</ div>
< div class = " container" >
< div class = " row" style =" margin-top : 70px" >
{% block page_main %}
页面代码位置
{% endblock %}
</ div>
</ div>
< script src = " /static/jQuery/jQuery3.4.1.min.js" > </ script>
< script src = " /static/bootstrap-3.3.7-dist/js/bootstrap.min.js" > </ script>
{% block page_js %}
单页面引入 js 文件位置
{% endblock %}
</ body>
</ html>
组件, 文件名为 zujian.html< p> 这是一个组件文件 </ p>
引入母版和组件
{ % extends "base.html" % }
{ % block page_title % }
主页
{ % endblock % }
{ % block page_main % }
< p> < a href= "/index/" > 主页< / a> < / p>
{ % include "zujian.html" % }
{ % endblock % }
使用实例
上传文件
views.pyclass Upload ( views. View) :
def get ( self, request) :
return render( request, "test.html" )
def post ( self, request) :
file_obj = request. FILES. get( "code" )
filename = file_obj. name
with open ( filename, "wb" ) as f:
for i in file_obj. chunks( ) :
f. write( i)
return HttpResponse( "上传成功" )
test.html<!DOCTYPE html>
< html lang = " en" >
< head>
< meta charset = " UTF-8" >
< title> Title</ title>
</ head>
< body>
< form action = " " method = " post" enctype = " multipart/form-data" >
< input type = " file" name = " code" >
< input type = " submit" value = " 提交" >
</ form>
</ body>
</ html>
多选示例
views.pyclass AuthorEdit ( views. View) :
def get ( self, request, id ) :
author = models. Author. objects. filter ( id = id ) . first( )
books = models. Book. objects. all ( )
return render( request, "author_edit.html" , { "author" : author, "books" : books} )
def post ( self, request, id ) :
name = request. POST. get( "author" )
books = request. POST. getlist( "books" )
author_obj = models. Author. objects. filter ( id = id ) . first( )
author_obj. name = name
author_obj. books. set ( books)
author_obj. save( )
return redirect( "/author_list/" )
author_edit.html{ % extends "mom.html" % }
{ % block html_title % }
编辑作者
{ % endblock % }
{ % block html_main % }
< form action= "" method= "post" >
{ % csrf_token % }
< p>
作者:
< input type = "text" name= "author" value= "{{ author.name }}" >
< / p>
< p>
书名:
< select name= "books" multiple>
{ % for foo in books % }
{ % if foo in author. books. all % }
< option selected value= "{{ foo.id }}" > { { foo. title } } < / option>
{ % else % }
< option value= "{{ foo.id }}" > { { foo. title } } < / option>
{ % endif % }
{ % endfor % }
< / select>
{ % for i in books % }
{ % if i in author. books. all % }
< p> < input checked type = "checkbox" name= "books" value= "{{ i.id }}" > { { i. title } } < / p>
{ % else % }
< p> < input type = "checkbox" name= "books" value= "{{ i.id }}" > { { i. title } } < / p>
{ % endif % }
{ % endfor % }
< / p>
< p> < input type = "submit" value= "修改" > < / p>
< / form>
{ % endblock % }