urls.py – 路由系統
基本使用
from django.urls import path
urlpatterns = [
path('login/', views.login),
]
def login(request):
return render(request, "login.html")
別名用法
from django.urls import path
urlpatterns = [
path('index/', views.index, name="index"),
]
from django.urls import reverse
def index(request):
return redirect(reverse("index"))
位置傳參
from django.urls import re_path
urlpatterns = [
re_path(r're1/(\d+)/(\d+)/$', views.re_1),
]
def re_1(request, arg1):
return HttpResponse(arg1)
關鍵字傳參
from django.urls import re_path
urlpatterns = [
re_path(r're2/(?P<re2_id>\d+)/$', views.re_2),
]
def re_2(request, re2_id):
return HttpResponse(re2_id)
include 用法
from django.urls import include
urlpatterns = [
path('stu/', include("stu.urls"))
]
urlpatterns = [
path('stu_add/', views.stu_add),
]
views.py – 視圖系統
FBV 和 CBV
from django import views
class exp1(views.View):
def get(self, request):
return HttpResponse("exp1.get")
def post(self, request):
return HttpResponse("exp1.post")
def exp2(request):
return HttpResponse("exp2")
urlpatterns = [
path('exp1', views.exp1.as_view())
path('exp2', views.exp2)
]
給 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
取多選數據
class 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/")
{% 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 %}
上傳文件
class Upload(views.View):
def get(self, request):
return render(request, "upload.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("上傳成功")
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="/upload/" method="post" enctype="multipart/form-data">
<input type="file" name="code">
<input type="submit" value="提交">
</form>
</body>
</html>
直接返回 json 結果
from django.http import JsonResponse
class JsonTest(views.View):
def get(self, request):
res = {"code": 0, "data": "alex"}
res2 = ["alex", "wusir", "mjj"]
return JsonResponse(res2, safe=False)
template – 模板系統
基本語法
{{ name }}
{{ obj.key }}
{{ ojb.index }}
顯示默認值
{{ data|default:"暫無數據" }}
轉換文件大小
filesize = 1024000
{{ filesize|filesizeformat }}
格式化時間
date = datetime.datetime.today()
{{ today|date:"Y-m-d H:i:s" }}
展示鏈接
link = "<a href='www.baidu.com>百度</a>"
{{ link|safe }}
for 循環
判斷是否爲最後一次
data = [1, 2, 3, 4]
{% for i in data %}
{% if forloop.last %}
{{ i }}
{% else %}
{{ i }},
{% endif %}
{% endfor %}
1,2,3,4
數據爲空顯示默認值
data = ""
{% for i in data %}
{{ i }}
{% empty %}
"默認值"
{% endfor %}
"默認值"
母版和組件
<!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>
<p> 這是一個組件文件 </p>
{% extends "base.html" %}
{% block page_title %}
主頁
{% endblock %}
{% block page_main %}
<p><a href="/index/">主頁</a></p>
{% include "zujian.html" %}
{% endblock %}