码迷,mamicode.com
首页 > 其他好文 > 详细

Xadmin 样式实现

时间:2019-08-19 09:26:03      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:type   模板   html   bsp   pre   stripe   head   使用   kconfig   

一、show_list

service\Xadmin组件

def list_view(self, request):
    # print(request.path)     # /Xadmin/app02/food/
    # print(self.model)   # <class ‘app02.models.Food‘>
    # 表身
    data_list = self.model.objects.all()
    new_data_list = []
    for obj in data_list:
        temp = []
        for field in self.list_display:     # ["title", ‘price‘] ["title", ‘price‘, edit]
            if isinstance(field, str):
                value = getattr(obj, field)
            else:
                value = field(self, obj)
            temp.append(value)
        new_data_list.append(temp)
    # new_data_list = [
    #     [‘python‘, 45],
    #     [‘go‘, 45]
    # ]
    # 表头
    head_list = []
    for filed in self.list_display:
        if isinstance(filed, str):
            if filed == "__str__":
                head_name = self.model._meta.model_name.capitalize()
            else:
                filed_obj = self.model._meta.get_field(filed)
                head_name = filed_obj.verbose_name
        else:
             head_name = filed(self, is_flag=True)
        head_list.append(head_name)
    return render(request,
                  "list_view.html",
                  {
                      "data_list": new_data_list,
                      "head_list": head_list
                  }
                  )

app中的Xadmin

class BookConfig(ModelXadmin):

    # 添加 href 用obj ,my_edit 方法在 样式类ModelXadmin被使用
    def my_edit(self, obj=None, is_flag=False):
        if is_flag:
            return "编辑操作"
        return mark_safe(<a href="%s/change/">编辑</a> % obj.pk)

    def my_delete(self, obj=None, is_flag=False):
        if is_flag:
            return "删除操作"
        return mark_safe(<a href="%s/delete/">删除</a>% obj.pk)

    def my_check(self, obj=None, is_flag=False):
        if is_flag:
            return "选择"
        return mark_safe(<input type="checkbox">)

    list_display = [my_check, "title", price, my_edit, my_delete]
    # list_display = ["title", ‘price‘]


site.register(Book, BookConfig)

3、对应的模板

<table class="table table-striped table-bordered">
    <thead>
        <tr>
            {% for head in head_list %}
                <th>{{ head }}</th>
            {% endfor %}

        </tr>
    </thead>
    <tbody>
        {% for data in data_list %}
            <tr>
                {% for i in data %}
                    <td>{{ i }}</td>
                {% endfor %}
            </tr>
        {% endfor %}
    </tbody>
</table>

 

Xadmin 样式实现

标签:type   模板   html   bsp   pre   stripe   head   使用   kconfig   

原文地址:https://www.cnblogs.com/wt7018/p/11375006.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!