Xadmin左侧菜单——后台菜单页面自定义

目录

1.菜单的自定义页面实现

2.models代码

3.adminx代码

4.template代码

5.实现效果

6.注意事项

7.后台页面中分页实现


1.菜单的自定义页面实现

此处页面自定义与我们实现html网页一样,都是通过MTV实现(urls+models+adminx+view+template)。不同之处在于,(1)view内的代码在adminx中实现;(2)url通过adminx注册生成可点击的菜单;(3)template页面需要继承xadmin/base_site.html。

2.models代码

class Worknews(models.Model):
    user = models.ForeignKey(User, verbose_name=u'记录人', on_delete=models.CASCADE, editable=False,null=True)  ## 记录创建该数据的用户
    start_time = models.DateTimeField(verbose_name=u'添加时间',auto_now_add=True)
    type = models.CharField(max_length=20,verbose_name=u'通知类型',choices=(('gsyw',u'公司要闻'),('gzdt',u'工作动态'),('hyzx',u'行业资讯')),default='hyzx')
    name = models.CharField(max_length=50,verbose_name=u'通知名称',default=u'')
    content = UEditorField(verbose_name=u'通知内容',width=800, height=600,toolbars='full',
                                         imagePath="Workafter/worknews/", filePath="Workafter/worknews/",
                                         upload_settings={"imageMaxSize":10204000},
                                         settings={},command=None,default='')

    class Meta:
        verbose_name = u'工作公告'
        verbose_name_plural = verbose_name
      
    def __str__(self):
        return self.name

3.adminx代码

from xadmin.views.base import CommAdminView
from .models import Notice_content

class Notice_contentAdmin(object):
    # 指向自定义的页面
    object_list_template = 'notice_list.html'
    #model_icon = 'fa fa-angle-double-right'  # >>图标
    # 重写方法,把要展示的数据更新到 context
    def get_context(self):
        context = CommAdminView.get_context(self)

        bill_message = Worknews.objects.all()
        print(bill_message)
        context.update(
            {
                'title': '最新通知',
                'bill_message': bill_message,
            }
        )
        return context

xadmin.site.register(Notice_content, Notice_contentAdmin)

4.template代码

{# 继承xadmin的侧边栏和导航栏 #}
{% extends 'xadmin/base_site.html' %}

#去掉标签栏
{% block content-nav %}
{% endblock %}

{% block content %}
    <div style = "height: 700px; width: 45%;margin-left: 20px;margin-top: 20px;margin-right: 20px;z-index: 0;color:brown;background: rgba(191,246,223,0.03)" align="left" >
            <h1>最新消息</h1>
            {% for i in bill_message %}
               <a href="content/{
   
   { i.id }}">
                <h5>
                    <div style="width: 95%;margin-left: 20px;margin-top: 20px;z-index: 0;color: black;" >
                        <div style="float:left">{
   
   { forloop.counter }}、</div>
                            <div style="float:left">{
   
   { i.name}}</div>
                            <div align="right">{
   
   { i.start_time}}</div>
                    </div>
                    <hr style="height:1px ;background: rgba(83,177,141,0.4)">
                </h5>
                </a>
            {% endfor %}
    </div>
{% endblock content %}

5.实现效果

6.注意事项

以上内容为不同之处,在实际应用中不可缺少settings设置和urls配置。

7.后台页面中分页实现

(1)xadmin代码

from xadmin.views.base import CommAdminView
from .models import Applicant_content
from pure_pagination import Paginator, EmptyPage, PageNotAnInteger
from django.http import HttpRequest

class Applicant_contentAdmin(object):
    # 指向自定义的页面
    object_list_template = 'operate_list.html'
    #model_icon = 'fa fa-angle-double-right'  # >>图标
    # 重写方法,把要展示的数据更新到 context
    def get_context(self):
        context = CommAdminView.get_context(self)

        operate_message = Applicant.objects.all().order_by('-save_time')
        try:
            page = self.request.GET.get('page',1)
        except PageNotAnInteger:
            page = 1
        p = Paginator(operate_message, 10, request=self.request)
        operate_messages = p.page(page)
        context.update(
            {
                'title': '最新通知',
                'operate_messages': operate_messages,
            }
        )
        return context
xadmin.site.register(Applicant_content, Applicant_contentAdmin)

(2)models代码

{# 继承xadmin的侧边栏和导航栏 #}
{% extends 'xadmin/base_site.html' %}

#去掉标签栏
{% block content-nav %}
{% endblock %}

{% block content %}

    <div style = "height: 650px; width: 45%;margin-left: 20px;margin-top: 20px;margin-right: 20px;z-index: 0;color:brown;background: rgba(191,246,223,0.03)" align="left" >
            <h1>最新消息</h1>
            {% for i in operate_messages.object_list %}
               <a href="content/{
   
   { i.id }}">
                <h5>
                    <div style="width: 95%;margin-left: 20px;margin-top: 20px;z-index: 0;color: black;" >
                        <div style="float:left">{
   
   { forloop.counter }}、</div>
                            <div style="float:left">{
   
   { i.company}}-</div>
                            <div style="float:left">{
   
   { i.department}}-</div>
                            <div style="float:left">{
   
   { i.add_post }}-</div>
                            <div style="float:left">到岗{
   
   { i.arrive_time }}</div>
                            <div align="right">申请{
   
   { i.save_time}}</div>
                    </div>
                    <hr style="height:1px ;background: rgba(83,177,141,0.4)">
                </h5>
                </a>
            {% endfor %}
        {% load i18n %}
        <div class="pagination" style="margin: auto;float: right" align="right">
            {% if operate_messages.has_previous %}
                <a href="?{
   
   { operate_messages.previous_page_number.querystring }}" class="long">{% trans "上一页" %}</a>
            {% else %}
                <span class="disabled long">{% trans "上一页" %}</span>
            {% endif %}
            {% for page in operate_messages.pages %}
                {% if page %}
                    {% ifequal page operate_messages.number %}
                        <span class="current page">{
   
   { page }}</span>
                    {% else %}
                        <a href="?{
   
   { page.querystring }}" class="page">{
   
   { page }}</a>
                    {% endifequal %}
                {% else %}
                    ...
                {% endif %}
            {% endfor %}
            {% if operate_messages.has_next %}
                <a href="?{
   
   { operate_messages.next_page_number.querystring }}" class="long">{% trans "下一页" %}</a>
            {% else %}
                <span class="disabled long">{% trans "下一页" %}</span>
            {% endif %}
        </div>
    </div>
{% endblock content %}

(3)分页重点pure_pagination

分页代码参考文章:https://blog.csdn.net/huoyuanshen/article/details/83310274
不同于常用分页,菜单自定义页面分页容易卡主的地方是adminx中如何获得request,此处是通过self获得。(但是在这里卡住了)

猜你喜欢

转载自blog.csdn.net/qq_15028721/article/details/108822049