django custom tag static component configuration file

Package

  1. Some functions will label written in a html file, this file as a component, if the files need to directly take over use;

    这是title.html文件,写了一个导航栏,作为一个公用的组件
    <div style="background-color: cyan;width: 100%;height: 40px;">
        <span style="font-size: 20px;">首页</span>
        <span style="font-size: 20px;">搜索</span>
        <span style="font-size: 20px;">登录</span>
        <span style="font-size: 20px;">注册</span>
    </div>
    
    引用方式:
    {% include 'title.html' %}
    将这个语句放在你html文件需要的位置;就会将这这个导航栏应用过来

Custom label

  1. First created in the app application below a folder ---- templatetags (must be the name can not be changed)

  2. In this folder inside python write their own logical file name at random, the name of this file is tag

    # 下面是一个自定义的过滤器
    from django import template
    # 导入django自带的包
    register = template.Library()  # register 名字不能改变,注册器
    #在你写的函数头部加上这个装饰器,这就是一个过滤器
    # 这是无参数的过滤器
    @register.filter
    def filter(n1):
        '''
        这是一个子自定义的过滤器,无参数的过滤器
        :param n1: 接收的在html文件中的过滤器中的管道符前边的那个变量的值
        :return:
        '''
        return n1 + '你好'
    
    # 这是有参数的过滤器
    @register.filter
    def filter2(n1,n2):
        '''
        这是一个子自定义的过滤器,有参数的过滤器
        :param n1: 接收的在html文件中的过滤器中的管道符前边的那个变量的值
        :param n2: 传入的参数,管道符后边的,如果不需要传参数就不要写,
        :return:
        '''
    
        return n1 + n2
  3. Application in the HTML file

    # 首先导入tag.py这个文件
    {% load tag %}
    
    # 无参数的使用方法
    <h1>{{ name|filter2}}</h1>
    
    # 有参数的使用方法
    <h1>{{ name|filter2:'你好坏'}}</h1>
  4. File the required parameters are passed in the view file

    def home(request):
     name = 'adrian'
        return render(request,'home.html',{'name':name})

inclusion_tag html code segment for returning ----

  1. This method is html page you need to return a fragment of html file, displays some data by rendering a template html file

    Application of a King is usually on the left side of the page pull-down bar, pull-down bar application of this general web pages and more, but the data are not the same, so we do use inclusion_tag handle different data requirements of different pages;

    img

  2. First, write a simple function

    from django import template
    register = template.Library()  # register 名字不能改变,注册器
    # 在这个函数的头部加一个有参装饰器
    @register.inclusion_tag('result.html')   
    # 这个装饰器的参数是一个html页面
    def inclusion():
    
        return {'l1':['抽烟','喝酒','烫头']}   
     # 在定义的这个字典中的键 l1 必须是result这个html文件中的for所有循环的列表名,这两个名字必须一致
  3. result of this html file code

    <ul>
        {% for i in l1 %}
            <li>{{ i }}</li>
        {% endfor %}
    </ul>
  4. How to use this approach?

    # 在你需要的网页上将这个文件导入tag.py 文件是你写函数的文件
    {% load tag %}
    
    # 将在你需要的地方写
    {% inclusion %}  
    # inclusion是你写的函数

Static configuration file

  1. The page need to add some css, js, pictures and other files, how to write the path to these files in django in it?

    #首先在你的项目文件里建立一个statics文件夹,在这个文件夹下在增加你需要的文件夹,比如css,js文件夹,在这些文件夹下写入对应的css,js文件,
  2. The second is to profile the settings in the configuration file django

    #关键的概念:Django中,静态资源的存放通过设置 STATIC_URL, STATICFILES_DIRS 来设置,一般STATIC_URL设置为:/static/    ----  这个名字就叫做静态文件路径别名,可以改变
    
    #STATIC_URL='/static/', 这个static 是在Django 具体APP下建立的statics目录,用来存放静态资源。而STATICFILES_DIRS一般用来设置通用的静态资源,例如:
    
    STATICFILES_DIRS=[os.path.join(BASE_DIR, "statics"),]
  3. How to use html file

    <link rel="stylesheet" href="/static/css/index.css">
    在插入css文件时只需要写你的路径别名,在加上css文件夹名和文件名

Guess you like

Origin www.cnblogs.com/zhufanyu/p/11665473.html