Django the custom label study notes, using filters (dynamic html tag class attribute assigned)

1, Django flowchart

 

2, a custom label, as a filter:

       When using django often encounter a situation that is passed from view to view the value of the template template requires further treatment to use in the template, but the template is not running python statement, such as encounter the following problems:

 Node.id view to pass a variable template, which need to interact in accordance with this value talk database, the result is to <span> class attribute assignment html tag in the front end, if the result is True, the assignment class = 'folder exp1', otherwise assigned class = 'file exp1'. Because the variable passed to the view template also need to be addressed, the need to interact with the outcome of the database, which is part of the function in html unenforceable.

So how to deal with it, then you need to use custom tags and filters up. The parameters passed to the filter in a custom label, complete interaction with the database in the filter, and returns a value.

 

3, the use of custom tags

1. Create a file called templatetags (essential and immutable package name) of the package, and then create a py file in this package at a certain app, the py file is your custom label document as follows rbac.py, directory as follows:

2. Add the name of APP in the settings file within INSTALLD_APPS, such as my name is rbvc AP, add rbvc in settings.py as follows:

3. Write Filter judgefileorfolder in rbac.py file


from django import template
from web.models import *


register = template.Library()

@register.filter(name='judgefileorfolder')
def judgefileorfolder(value):
    def get_upper_business(objects):
        return Business_detail.objects.get(id=objects.upper_business)
    objects=Business_detail.objects.get(id=value)
    try:
        for i in range(3):
            objects=get_upper_business(objects)
        if not objects.upper_business:
            sign = False
        else:
            sign = True
    except:
        sign=True
    return sign

Custom filter:

  1.   Custom filters is to write a function
  2.  Filter is used to filter the values ​​passed to the function
  3.  After the function, you need to be registered register, and returns the value to the template

4. Call custom tags in the template and filters

{% load rbac %}  <!-- 载入自定义标签 -->
<style type="text/css">
.exp1{font-size:15px;}
</style>
<li><span id="business_{{ node.id }}"  {%  if node.id|judgefileorfolder %} class="folder exp1"
                                        {% else %} class="file exp1"
                                        {% endif %}>  {{ node.name }} </span>  <!-- 渲染当前遍历的节点 -->
    {% if node.id|getNextBusiness %}  <!-- 通过过滤器, 判断得到当前业务节点是否有下级业务-->
        <ul>
        {% for node in node.id|getNextBusiness %}  <!-- 遍历当前业务节点的下级业务节点 -->
            {% include "businessNode.html" %}  <!-- 再次引入自身页面、判断节点、遍历节点等等, 实现深层迭代得到多级业务节点 -->
        {% endfor %}
        </ul>
    {% endif %}
</li>

Now template is started by {% load rbac%} Loading custom label, then the filter template can be used judgefileorfolder, in place of use by node.id | embodiment will be passed to the filter variable name | judgefileorfolder, i.e., the variable name filter. Django through the built-in template tag {% if%} {% endif%} That can be done by determining whether the return value to the dynamic filters <span> class assignment Properties tab.

 

Published 24 original articles · won praise 30 · views 50000 +

Guess you like

Origin blog.csdn.net/yufen9987/article/details/90270833