社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Django

如何设计组织Django模板?

马哥Linux运维 • 2 年前 • 385 次点击  

在本文中,我将介绍在Django应用程序开发中设计Django模板的方法。目的是保持Django应用程序的UI部分井井有条,并避免重复编码。Django在模板引擎中提供了各种机制来帮助我们实现这一目标。在本教程中,我将说明如何使用Django内置模板标记块,扩展和包含来使模板易于维护。

准备工作:

1、Python 3.6

2、Django 2.2

3、AdminLTE 3.0.5

我们目标是将模板文件有效组织起来,避免重复的代码引用,我们分四个步骤来实现。

步骤1/4:base.html

将模板分为多个部分,我们知道除了菜单和内容外,其他所有内容都是可重复的。我们将制作一个基本模板来容纳那些常见的部分,如图:

在项目文件夹中创建一个文件夹模板。在其中创建一个base.html。将所有常见的片段添加到其中。只需复制并粘贴以下内容,仅是load.html和index.html共享的一部分代码。

{% load static %}

html>

"en">

  "utf-8">
  "viewport" content="width=device-width, initial-scale=1">
  "x-ua-compatible" content="ie=edge">

  AdminLTE 3 | Starter

  
  "stylesheet" href="{% static 'plugins/fontawesome-free/css/all.min.css' %}">
  
  "stylesheet" href="{% static 'dist/css/adminlte.min.css' %}">
  
  "https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,400i,700" rel="stylesheet">

"hold-transition sidebar-mini">
"wrapper">

  
  
  

  
  

  
  {% block content_wrapper %}{% endblock %}
  

  
  
  

  
  
"main-footer">
    
    
"float-right d-none d-sm-inline">
      Anything you want
    

    
    Copyright © 2014-2019 "https://adminlte.io">AdminLTE.io. All rights reserved.
  














请注意,块content_wrapper用于呈现每个页面的自定义内容。

步骤2/4:删除冗余的通用代码

由于我们在上一步中创建了base.html,因此不再需要将通用代码保留在Landing.html和home.html中。我们应该得到如下结果。

"content-wrapper">
  
  
"content-header">
    
"container-fluid">
      
"row mb-2">
        
"col-sm-6">
          

"m-0 text-dark">Polls Index Page


        

        
"col-sm-6">
          
    "breadcrumb float-sm-right">
                
  1. "breadcrumb-item">"#">Home

  2.             
  3. "breadcrumb-item active">Polls

  4.           

        

      

    

  

  

  
  
"content">
    
"container-fluid">
      
"row">
        
"col-lg-6">
          
"card">
            
"card-body">
              
"card-title">Card title


              

"card-text">
                Some quick example text to build on the card title and make up the bulk of the card's
                content.
              



              Card link
              Another link
            

          


          

            

              
Card title


              


                Some quick example text to build on the card title and make up the bulk of the card's
                content.
              


              "#" class="card-link">Card link
              "#" class="card-link">Another link
            

          


          
          
"card card-primary card-outline">
            
"card-header">
              

"card-title">General Elements


            

            
            
"card-body">
              
"form">
                
"row">
                  
"col-sm-6">
                    
                    
"form-group">
                      
                      type="text" class="form-control" placeholder="Enter ...">
                    

                  

                  
"col-sm-6">
                    
"form-group">
                      
                      type="text" class="form-control" placeholder="Enter ..." disabled>
                    

                  

                

                
"row">
                  
"col-sm-6">
                    
                    
"form-group">
                      
                      
                    

                  

                  
"col-sm-6">
                    
"form-group">
                      
                      
                    

                  

                


                
                
"form-group">
                  
                  type="text" class="form-control is-valid" id="inputSuccess" placeholder="Enter ...">
                

                
"form-group">
                  
                  type="text" class="form-control is-warning" id="inputWarning" placeholder="Enter ...">
                

                
"form-group">
                  
                  type="text" class="form-control is-invalid" id="inputError" placeholder="Enter ...">
                


                
"row">
                  
"col-sm-6">
                    
                    
"form-group">
                      
"form-check">
                        "form-check-input" type="checkbox">
                        
                      

                      
"form-check">
                        "form-check-input" type="checkbox" checked>
                        
                      

                      
"form-check">
                        "form-check-input" type="checkbox" disabled>
                        
                      

                    

                  

                  
"col-sm-6">
                    
                    
"form-group">
                      
"form-check">
                        "form-check-input" type="radio" name="radio1">
                        
                      

                      
"form-check">
                        "form-check-input" type="radio" name="radio1" checked>
                        
                      

                      
"form-check">
                        "form-check-input" type="radio" disabled>
                        
                      

                    

                  

                


                
"row">
                  
"col-sm-6">
                    
                    
"form-group">
                      
                      
                    

                  

                  
"col-sm-6">
                    
"form-group">
                      
                      
                    

                  

                


                
"row">
                  
"col-sm-6">
                    
                    
"form-group">
                      
                      
                    

                  

                  
"col-sm-6">
                    
"form-group">
                      
                      
                    

                  

                

              

            

            
          

          
        

        
        
"col-lg-6">
          
"card">
            
"card-header">
              
"m-0">Featured

            

            
"card-body">
              
"card-title">Special title treatment


              

"card-text">With supporting text below as a natural lead-in to additional content.


              "#" class="btn btn-primary">Go somewhere
            

          


          
"card card-primary card-outline">
            
"card-header">
              
"m-0">Featured

            

            
"card-body">
              
"card-title">Special title treatment


              

"card-text">With supporting text below as a natural lead-in to additional content.


              "#" class="btn btn-primary">Go somewhere
            

          

        

        
      

      
    

  

  

landing.html页面代码:

"content-wrapper">
  
  
"content-header">
    
"container-fluid">
      
"row mb-2">
        
"col-sm-6">
          

"m-0 text-dark">Home Landing Page


        

        
"col-sm-6">
          
    "breadcrumb float-sm-right">
                
  1. "breadcrumb-item">"#">Landing Page

  2.           

        

      

    

  

  

  
  
"content">
    
"container-fluid">
      
"row">
        
"col-lg-6">
          
"card">
            
"card-body">
              
"card-title">Card title


              

"card-text">
                Some quick example text to build on the card title and make up the bulk of the card's
                content.
              



              Card link
              Another link
            

          


          

            

              
Card title


              


                Some quick example text to build on the card title and make up the bulk of the card's
                content.
              


              "#" class="card-link">Card link
              "#" class="card-link">Another link
            

          

        

        
"col-md-6">
          
          
"card card-warning">
            
"card-header">
              

"card-title">General Elements


            

            
            
"card-body">
              
"form">
                
"row">
                  
"col-sm-6">
                    
                    
"form-group">
                      
                      type="text" class="form-control" placeholder="Enter ...">
                    

                  

                  
"col-sm-6">
                    
"form-group">
                      
                      type="text" class="form-control" placeholder="Enter ..." disabled>
                    

                  

                

                
"row">
                  
"col-sm-6">
                    
                    
"form-group">
                      
                      
                    

                  

                  
"col-sm-6">
                    
"form-group">
                      
                      
                    

                  

                


                
                
"form-group">
                  
                  type="text" class="form-control is-valid" id="inputSuccess" placeholder="Enter ...">
                

                
"form-group">
                  
                  type="text" class="form-control is-warning" id="inputWarning" placeholder="Enter ...">
                

                
"form-group">
                  
                  type="text" class="form-control is-invalid" id="inputError" placeholder="Enter ...">
                


                
"row">
                  
"col-sm-6">
                    
                    
"form-group">
                      
"form-check">
                        "form-check-input" type="checkbox">
                        
                      

                      
"form-check">
                        "form-check-input" type="checkbox" checked>
                        
                      

                      
"form-check">
                        "form-check-input" type="checkbox" disabled>
                        
                      

                    

                  

                  
"col-sm-6">
                    
                    
"form-group">
                      
"form-check">
                        "form-check-input" type="radio" name="radio1">
                        
                      

                      
"form-check">
                        "form-check-input" type="radio" name="radio1" checked>
                        
                      

                      
"form-check">
                        "form-check-input" type="radio" disabled>
                        
                      

                    

                  

                


                
"row">
                  
"col-sm-6">
                    
                    
"form-group">
                      
                      
                    

                  

                  
"col-sm-6">
                    
"form-group">
                      
                      
                    

                  

                


                
"row">
                  
"col-sm-6">
                    
                    
"form-group">
                      
                      
                    

                  

                  
"col-sm-6">
                    
"form-group">
                      
                      
                    

                  

                

              

            

            
          

          
        

      

    

  

  

步骤3/4:继承base.html

为了将base.html用作每个页面的基础模板,我们需要通过在模板的开头使用{%extended‘base.html’%}来声明base.html为“父”模板。最重要的是,不要忘记content_wrapper块。将全部内容包装到该块中。我们应该得到如下结果。

landing.html:

{% extends 'base.html' %}
{% load static %}
{% block content_wrapper %}
  
"content-wrapper">
    . . .
  

{% endblock %}

在index.html:

{% extends 'base.html' %}
{% load static %}
{% block content_wrapper %}
  
"content-wrapper">
    . . .
  

{% endblock %}

步骤4/4:将常见的内容单独存放

现在我们可能会意识到,两个模板中都存在相同的巨型形式。几乎一半的代码是它。由于此表单已在两个模板中重复使用,因此我们将其维护在一个可以包含任何模板的地方。

在模板文件夹中创建一个文件夹advanced_forms。在advanced_forms文件夹中,创建如下的general_elements_form.html,代码如下:

"form">
  
"row">
    
"col-sm-6">
      
      
"form-group">
        
        type="text" class="form-control" placeholder="Enter ...">
      

    

    
"col-sm-6">
      
"form-group">
        
        type="text" class="form-control" placeholder="Enter ..." disabled>
      

    

  

  
"row">
    
"col-sm-6">
      
      
"form-group">
        
        
      

    

    
"col-sm-6">
      
"form-group">
        
        
      

    

  


  
  
"form-group">
    
    type="text" class="form-control is-valid" id="inputSuccess" placeholder="Enter ...">
  

  
"form-group">
    
    type="text" class="form-control is-warning" id="inputWarning" placeholder="Enter ...">
  

  
"form-group">
    
    type="text" class="form-control is-invalid" id="inputError" placeholder="Enter ...">
  


  
"row">
    
"col-sm-6">
      
      
"form-group">
        
"form-check">
          "form-check-input" type="checkbox">
          
        

        
"form-check">
          "form-check-input" type="checkbox" checked>
          
        

        
"form-check">
          "form-check-input" type="checkbox" disabled>
          
        

      

    

    
"col-sm-6">
      
      
"form-group">
        
"form-check">
          "form-check-input" type="radio" name="radio1">
          
        

        
"form-check">
          "form-check-input" type="radio" name="radio1" checked>
          
        

        
"form-check">
          "form-check-input" type="radio" disabled>
          
        

      

    

  


  
"row">
    
"col-sm-6">
      
      
"form-group">
        
        
      

    

    
"col-sm-6">
      
"form-group">
        
        
      

    

  


  
"row">
    
"col-sm-6">
      
      
"form-group">
        
        
      

    

    
"col-sm-6">
      
"form-group">
        
        
      

    

  


删除Landing.html和index.html中的多余表单代码。使用{% include ‘advanced_forms/general_elements_form.html’ %}包含表单。设置好之后,这就是最终结果。

index.html:

{% extends 'base.html' %}
{% load static %}

{% block content_wrapper %}
  
"content-wrapper">
    
    
"content-header">
      
"container-fluid">
        
"row mb-2">
          
"col-sm-6">
            

"m-0 text-dark">Polls Index Page


          

          
"col-sm-6">
            
    "breadcrumb float-sm-right">
                  
  1. "breadcrumb-item">"#">Home

  2.               
  3. "breadcrumb-item active">Polls

  4.             

          

        

      

    

    

    
    
"content">
      
"container-fluid">
        
"row">
          
"col-lg-6">
            
"card">
              
"card-body">
                
"card-title">Card title


                

"card-text">
                  Some quick example text to build on the card title and make up the bulk of the card's
                  content.
                



                Card link
                Another link
              

            


            

              

                
Card title


                


                  Some quick example text to build on the card title and make up the bulk of the card's
                  content.
                


                "#" class="card-link">Card link
                "#" class="card-link">Another link
              

            


            
            
"card card-primary card-outline">
              
"card-header">
                

"card-title">General Elements


              

              
              
"card-body">
                {% include 'advanced_forms/general_elements_form.html' %}
              

              
            

            
          

          
          
"col-lg-6">
            
"card">
              
"card-header">
                
"m-0">Featured

              

              
"card-body">
                
"card-title">Special title treatment


                

"card-text">With supporting text below as a natural lead-in to additional content.


                "#" class="btn btn-primary">Go somewhere
              

            


            
"card card-primary card-outline">
              
"card-header">
                
"m-0">Featured

              

              
"card-body">
                
"card-title">Special title treatment


                

"card-text">With supporting text below as a natural lead-in to additional content.


                "#" class="btn btn-primary">Go somewhere
              

            

          

          
        

        
      

    

    
  

{% endblock %}
loading.html:

{% extends 'base.html' %}
{% load static %}

{% block content_wrapper %}
  
"content-wrapper">
    
    
"content-header">
      
"container-fluid">
        
"row mb-2">
          
"col-sm-6">
            

"m-0 text-dark">Home Landing Page


          

          
"col-sm-6">
            
    "breadcrumb float-sm-right">
                  
  1. "breadcrumb-item">"#">Landing Page

  2.             

          

        

      

    

    

    
    
"content">
      
"container-fluid">
        
"row">
          
"col-lg-6">
            
"card">
              
"card-body">
                
"card-title">Card title


                

"card-text">
                  Some quick example text to build on the card title and make up the bulk of the card's
                  content.
                



                Card link
                Another link
              

            


            

              

                
Card title


                


                  Some quick example text to build on the card title and make up the bulk of the card's
                  content.
                


                "#" class="card-link">Card link
                "#" class="card-link">Another link
              

            

          

          
"col-md-6">
            
            
"card card-warning">
              
"card-header">
                

"card-title">General Elements


              

              
              
"card-body">
                {% include 'advanced_forms/general_elements_form.html' %}
              

              
            

            
          

        

      

    

    
  

{% endblock %}

现在,让我们再次重新启动项目。屏幕上没有任何变化。但是从整体结构上,项目变得更易于维护。努力将是值得的(笑脸) 这是我们完成增强后的文件架构的目录结构。我用黄色突出显示了模板。

写在最后,模板是Web应用程序中的核心部分之一。不要写重复代码,我认为该原则适用于前端和后端开发,这样我们才可以制作出可伸缩的应用程序。

文章转载:Python运维技术
(版权归原作者所有,侵删)

点击下方“阅读原文”查看更多

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/117346
 
385 次点击