Xadmin 样式实现
Posted wt7018
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Xadmin 样式实现相关的知识,希望对你有一定的参考价值。
一、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 样式实现的主要内容,如果未能解决你的问题,请参考以下文章
django-xadmin自定义widget插件(自定义详情页字段的显示样式)