django show the front page of note:

django show the front page Note:

  1. Ordinary field

    Object field name - "the data in the database

  2. There are choices of field parameters

    The object field - "Data database

    对象.get_字段名_display() - "The results show that the

  3. Custom method

     def show_class(self):
         return ','.join([str(i) for i in self.class_list.all()])
    当遇到多对多时,要使用该自定义方法来展示多个数据  

4. When the front end of the return data is determined to be done, can be returned directly from the rear end of the front end of the code, but is to be noted, at the front end to add | safe data representing security, before the data is returned at the rear end or else put mark_safe ()

from django.utils.safestring import mark_safe

def show_status(self):    
    color_dict={        'have':"#999999",        'lend':"red"    } 设值不同的选项对应的颜色
    return mark_safe('<span style="background-color: {};color:black;padding: 3px">{}</span>'.format(color_dict.get(self.book_state),self.get_book_state_display()))
返回前端数据 ,在数据前面套mark_safe()  
给选项(有choices参数)的数据返回不同的效果可以参考该方法.

Guess you like

Origin www.cnblogs.com/lingege/p/12431996.html