search
HomeWeb Front-endLayui TutorialDjango+Layui background layout introduction

Django+Layui background layout introduction

Layui background layout:

1. Top sidebar of background layout

<div class="layui-header">
        <div class="layui-logo">layui 后台布局</div>
        <!-- 头部区域(可配合layui已有的水平导航) -->
        <ul class="layui-nav layui-layout-left">
            <li class="layui-nav-item"><a href="">控制台</a></li>
            <li class="layui-nav-item"><a href="">商品管理</a></li>
            <li class="layui-nav-item"><a href="">用户</a></li>
            <li class="layui-nav-item">
                <a href="javascript:;">其它系统</a>
                <dl class="layui-nav-child">
                    <dd><a href="">邮件管理</a></dd>
                    <dd><a href="">消息管理</a></dd>
                    <dd><a href="">授权管理</a></dd>
                </dl>
            </li>
        </ul>
        <ul class="layui-nav layui-layout-right">
            <li class="layui-nav-item">
                <a href="javascript:;">
                    <img  class="layui-nav-img lazy"  src="/static/imghwm/default1.png"  data-src="http://t.cn/RCzsdCq"    alt="Django+Layui background layout introduction" >
                    {{request.user.username}}
                </a>
                <dl class="layui-nav-child">
                    <dd><a href="">基本资料</a></dd>
                    <dd><a href="">安全设置</a></dd>
                </dl>
            </li>
            <li class="layui-nav-item"><a href="">退了</a></li>
        </ul>
    </div>

The line with orange background in the code is the current login passed in Django The user's username.

The final effect is as follows:

Django+Layui background layout introduction

2. Sidebar of background layout

The sidebar needs to dynamically load nodes and sub-nodes. Therefore, you need to first establish the model of nodes and sub-nodes in Django. The model code is as follows:

# 节点类
class NavigationProfile(models.Model):
    name=models.CharField(max_length=20,verbose_name=&#39;节点名称&#39;)
    url=models.CharField(max_length=50,verbose_name=&#39;链接地址&#39;)
    iconCls=models.CharField(max_length=50,verbose_name=&#39;图标icon&#39;)

    class Meta:
        verbose_name=&#39;节点名称&#39;
        verbose_name_plural=verbose_name

    def __str__(self):
        return self.name

# 子节点类
class NavigationSubProfile(models.Model):
    name=models.CharField(max_length=20,verbose_name=&#39;子节点名称&#39;)
    url=models.CharField(max_length=50,verbose_name=&#39;链接地址&#39;)
    iconCls=models.CharField(max_length=50,verbose_name=&#39;图标icon&#39;)
    parent=models.ForeignKey(NavigationProfile,on_delete=models.CASCADE,verbose_name=&#39;父节点&#39;)

    class Meta:
        verbose_name=&#39;子节点名称&#39;
        verbose_name_plural=verbose_name

    def __str__(self):
        return self.name

The code in Django views is as follows:

def open_index(request):
    if request.user.is_authenticated == False:
        return HttpResponseRedirect(&#39;/account/login/&#39;)
    else:
        navigation = NavigationProfile.objects.all()
        dict = []

        for nav in navigation:
            dic = {}
            id = nav.id
            dic[&#39;id&#39;] = nav.id
            dic[&#39;text&#39;] = nav.name
            dic[&#39;url&#39;] = nav.url
            dic[&#39;iconCls&#39;] = nav.iconCls
            dic[&#39;nid&#39;] = 0
            sub_navigation = NavigationSubProfile.objects.filter(parent_id=id)

            sub_dict = []
            for sub_nav in sub_navigation:
                sub_dic = {}
                sub_dic[&#39;id&#39;] = sub_nav.id
                sub_dic[&#39;text&#39;] = sub_nav.name
                sub_dic[&#39;url&#39;] = sub_nav.url
                sub_dic[&#39;iconCls&#39;] = sub_nav.iconCls
                sub_dic[&#39;nid&#39;] = sub_nav.parent_id
                sub_dict.append(sub_dic)

            dic["children"] = sub_dict
            dict.append(dic)30         return render(request,&#39;index.html&#39;,{&#39;dict&#39;:dict})

The Django urls code is as follows:

 urlpatterns = [
     path(&#39;get-navigationProfile/&#39;,views.get_navigationProfile,name=&#39;get_navigationProfile&#39;),
 ]

The Html sidebar code is as follows:

<div class="layui-side layui-bg-black">
        <div class="layui-side-scroll">
            <!-- 左侧导航区域(可配合layui已有的垂直导航) -->
            <ul class="layui-nav layui-nav-tree" lay-filter="test">

                {% for dic in dict %}
                    <li class="layui-nav-item">
                        <a class="" href="javascript:;">{{ dic.text }}</a>
                        <dl class="layui-nav-child">
                            {% for child in dic.children %}
                                <dd><a href="javascript:;" data-id="{{ child.id }}"
                                       data-url="{{ child.url }}">{{ child.text }}</a></dd>
                            {% endfor %}

                        </dl>
                    </li>
                {% endfor %}

            </ul>
        </div>
    </div>

Final rendering

Django+Layui background layout introduction

For more layui knowledge, please pay attention to the layui usage tutorial column.

The above is the detailed content of Django+Layui background layout introduction. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:博客园. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools