最新要闻

广告

手机

iphone11大小尺寸是多少?苹果iPhone11和iPhone13的区别是什么?

iphone11大小尺寸是多少?苹果iPhone11和iPhone13的区别是什么?

警方通报辅警执法直播中被撞飞:犯罪嫌疑人已投案

警方通报辅警执法直播中被撞飞:犯罪嫌疑人已投案

家电

Django-4

来源:博客园


(相关资料图)

Django-4

1.url的路由系统

1、url(r"^index/", views.index),    url(r"^home/", views.Home.as_view()),
2、url(r"^detail-(\d+).html", views.detail),  
3、url(r"^detail-(?P\d+)-(?P\d+).html", views.detail)PS:def detail(request, *args,**kwargs):pass实战:a. url(r"^detail-(\d+)-(\d+).html", views.detail),def func(request, nid, uid):pass                def func(request, *args):args = (2,9)def func(request, *args, **kwargs):args = (2,9)b. url(r"^detail-(?P\d+)-(?P\d+).html", views.detail)def func(request, nid, uid):passdef funct(request, **kwargs):kwargs = {"nid": 1, "uid": 3}def func(request, *args, **kwargs):args = (2,9)
4、 name    对URL路由关系进行命名, ***** 以后可以根据此名称生成自己想要的URL *****    url(r"^asdfasdfasdf/", views.index, name="i1"),    url(r"^yug/(\d+)/(\d+)/", views.index, name="i2"),    url(r"^buy/(?P\d+)/(?P\d+)/", views.index, name="i3"),    def func(request, *args, **kwargs):        from django.urls import reverse        url1 = reverse("i1")                              # asdfasdfasdf/        url2 = reverse("i2", args=(1,2,))                 # yug/1/2/        url3 = reverse("i3", kwargs={"pid": 1, "nid": 9}) # buy/1/9/        xxx.html        {% url "i1" %}               # asdfasdfasdf/        {% url "i2" 1 2 %}           # yug/1/2/        {% url "i3" pid=1 nid=9 %}   # buy/1/9/        注:        # 当前的URL        request.path_info 
5、多级路由    project/urls.py    from django.conf.urls import url,include    from django.contrib import admin    urlpatterns = [        url(r"^cmdb/", include("app01.urls")),        url(r"^monitor/", include("app02.urls")),    ]    app01/urls.py    from django.conf.urls import url,include    from django.contrib import admin    from app01 import views    urlpatterns = [        url(r"^login/", views.login),    ]    app02/urls.py    from django.conf.urls import url,include    from django.contrib import admin    from app02 import views    urlpatterns = [        url(r"^login/", views.login),    ]
6. 默认值url(r"^index/", views.index, {"name": "root"}),def index(request,name):print(name)return HttpResponse("OK")
7. 命名空间/admin/    include("app01.urls",namespace="m1")/crm/      include("app01.urls",namespace="m1")app01.urls/index/    name = "n1"reverser("m1:n1")

2.视图函数获取相关信息

request.environrequest.environ["HTTP_USER-AGENT"] -> UA# 请求的所有信息都封装在enviroment中

3.模版继承. 导入和自定义

1.模版继承

{% extends "master.html" %}{% block content %}...  -> 这里写内容{% endblock %}

2.模版导入

{% include "tag.html" %}

3.模版中自定义函数

1.simple_tag

1. app下创建templatetags目录2. 任意xxoo.py文件3. 创建template对象 registerfrom django import template    register = template.Library()        @register.simple_tag    def func(arg1, arg2, arg3):        return ....  # settings.py中App_Installed里面添加app.templatetags4. 应用在HTML中1> 顶部 {% load xxoo %}    2> {% 函数名 arg1 arg2 arg3 ... %}# 缺点: 不能作为if条件# 有点: 参数任意个数

2.filter

1. app下创建templatetags目录2. 任意xxoo.py文件3. 创建函数from django import template    register = template.Library()        @register.filter    def func(arg1, arg2):  # 只能是两个参数        return ...4. 顶部 {% load xxoo %}   {{ "..."|函数名: "..." }}  # 应用方式不一样. 为arg1和arg2传参    # 缺点: 两个参数. 不能加空格# 优点: 可以作为if条件

4.XSS问题

一种注入式攻击脚本. 发布评论等方式将事先准备好的恶意脚本注入到那些良性可信的网站中,当其他用户进入该网站后,脚本就在用户不知情的情况下偷偷地执行了,这样的脚本可能会窃取用户的信息、修改页面内容、或者伪造用户执行其他操作等等,后果不可估量

页面一般会自动防止xss攻击. 所以我们在后端传入的字符串也会被当做非法字符串

解决: 1> 前端界面加上 {{ page_str|safe }}

​2> 后台: from django.utils.safestring import mark_safe

​ make_safe(page_str)

5.自定义分页

封装了一个分页的模块. 可直接调用

传入 当前页, 数据条数, 每页个数, 分页显示个数, base_url

__author__ = "Administrator"from django.utils.safestring import mark_safeclass Page:    def __init__(self, current_page, data_count, per_page_count=10, pager_num=7):        self.current_page = current_page        self.data_count = data_count        self.per_page_count = per_page_count        self.pager_num = pager_num    @property    def start(self):        return (self.current_page - 1) * self.per_page_count    @property    def end(self):        return self.current_page * self.per_page_count    @property    def total_count(self):        v, y = divmod(self.data_count, self.per_page_count)        if y:            v += 1        return v    def page_str(self, base_url):        page_list = []        if self.total_count < self.pager_num:            start_index = 1            end_index = self.total_count + 1        else:            if self.current_page <= (self.pager_num + 1) / 2:                start_index = 1                end_index = self.pager_num + 1            else:                start_index = self.current_page - (self.pager_num - 1) / 2                end_index = self.current_page + (self.pager_num + 1) / 2                if (self.current_page + (self.pager_num - 1) / 2) > self.total_count:                    end_index = self.total_count + 1                    start_index = self.total_count - self.pager_num + 1        if self.current_page == 1:            prev = "上一页"        else:            prev = "上一页" % (base_url, self.current_page - 1,)        page_list.append(prev)        for i in range(int(start_index), int(end_index)):            if i == self.current_page:                temp = "%s" % (base_url, i, i)            else:                temp = "%s" % (base_url, i, i)            page_list.append(temp)        if self.current_page == self.total_count:            nex = "下一页"        else:            nex = "下一页" % (base_url, self.current_page + 1,)        page_list.append(nex)        jump = """        GO        <script>            function jumpTo(ths,base){                var val = ths.previousSibling.value;                location.href = base + val;            }        </script>        """ % (base_url,)        page_list.append(jump)        page_str = mark_safe("".join(page_list))        return page_str

关键词: