在Django中创建和使用MasterPages (Creating and Using MasterPages in Django)
MasterPages are a type Template that are used to implement common design across the web applications.
MasterPages是一种Template类型,用于在整个Web应用程序中实施通用设计。
Step 1: Create Sandbox, Activate it, Install Django 1.9, Create Sample Project
第1步:创建沙箱,将其激活,安装Django 1.9,创建示例项目
![creating and using masterpages 1](https://www.includehelp.com/Django/Images/creating-and-using-masterpages-1.jpg)
![creating and using masterpages 2](https://i-blog.csdnimg.cn/blog_migrate/dfd954ddf7ae60520054c2bfa1a01d10.png)
![creating and using masterpages 3](https://www.includehelp.com/Django/Images/creating-and-using-masterpages-3.jpg)
![creating and using masterpages 4](https://www.includehelp.com/Django/Images/creating-and-using-masterpages-4.jpg)
Step 2: Select Project Folder i.e. myapp (Outer One) and add new folder with name "templates".
步骤2:选择项目文件夹,即myapp (外部文件夹),然后添加名称为“ templates”的新文件夹。
![creating and using masterpages 5](https://i-blog.csdnimg.cn/blog_migrate/c970b7ff4de6282b495e64e6fb96e3e7.png)
![creating and using masterpages 6](https://www.includehelp.com/Django/Images/creating-and-using-masterpages-6.jpg)
Step 3: This templates folder is root location for all html templates. Add a base template i.e. base.html in this folder.
步骤3:此模板文件夹是所有html模板的根目录。 在此文件夹中添加基本模板,即base.html。
![creating and using masterpages 7](https://i-blog.csdnimg.cn/blog_migrate/225046bff2ef69f291ad4b7d6deadad9.png)
Step 4: I divided Base template (Master page) into 4 Sections i.e.
步骤4:我将基本模板(母版页)分为4个部分,即
![creating and using masterpages 8](https://www.includehelp.com/Django/Images/creating-and-using-masterpages-8.jpg)
Different Sections of Body Tag on base.html
base.html上身体标签的不同部分
![creating and using masterpages 9](https://i-blog.csdnimg.cn/blog_migrate/9ec85988eb3c50152df0801df22eb20a.png)
![creating and using masterpages 10](https://www.includehelp.com/Django/Images/creating-and-using-masterpages-10.jpg)
![creating and using masterpages 11](https://www.includehelp.com/Django/Images/creating-and-using-masterpages-11.jpg)
![creating and using masterpages 12](https://www.includehelp.com/Django/Images/creating-and-using-masterpages-12.jpg)
Step 5: Add Child Pages which are inherited from base.html and add their individual contents.
步骤5:添加从base.html继承的子页面,并添加其各自的内容。
![creating and using masterpages 13](https://www.includehelp.com/Django/Images/creating-and-using-masterpages-13.jpg)
![creating and using masterpages 14](https://i-blog.csdnimg.cn/blog_migrate/fbf1c1ac41fab5c0320c3d29baecf02d.png)
![creating and using masterpages 15](https://i-blog.csdnimg.cn/blog_migrate/b38b2407c553ebc39f5176b3baeb6190.png)
![creating and using masterpages 16](https://www.includehelp.com/Django/Images/creating-and-using-masterpages-16.jpg)
![creating and using masterpages 17](https://www.includehelp.com/Django/Images/creating-and-using-masterpages-17.jpg)
![creating and using masterpages 18](https://i-blog.csdnimg.cn/blog_migrate/71cc86fd6d1681faee96a1cfc74a0786.png)
![creating and using masterpages 19](https://i-blog.csdnimg.cn/blog_migrate/b0fc616c36d5927d308de28e4eebc52c.png)
Step 6: Add templates location to setting.py in admin app i.e. myapp (inner one)
第6步:在管理应用程序(即myapp)(内部程序)中将模板位置添加到setting.py
![creating and using masterpages 20](https://i-blog.csdnimg.cn/blog_migrate/7d9fabd183ddafd502f49fdf1369c615.png)
![creating and using masterpages 21](https://i-blog.csdnimg.cn/blog_migrate/887a8f05c5f4d57fffc4ab749c26e8d8.png)
Step 7: Add Controller (i.e. views.py) in Admin App (i.e. myapp[inner one])
第7步:在Admin App(即myapp [inner one])中添加控制器(即views.py)
![creating and using masterpages 22](https://i-blog.csdnimg.cn/blog_migrate/375de03bb6098815e380090d3b79ade7.png)
Step 8: Add Controller Action Methods in Controller (i.e. views.py)
步骤8:在Controller中添加Controller Action Methods(即views.py)
![creating and using masterpages 23](https://www.includehelp.com/Django/Images/creating-and-using-masterpages-23.jpg)
Step 9: Map routes to action methods in urls.py of admin app (i.e. myapp[inner])
第9步:在管理应用程序的urls.py(即myapp [inner])中将路由映射到操作方法
![creating and using masterpages 24](https://www.includehelp.com/Django/Images/creating-and-using-masterpages-24.jpg)
![creating and using masterpages 25](https://i-blog.csdnimg.cn/blog_migrate/1e1623b704f4975a41d6bab856160cb5.png)
Step 10: Run the Server
步骤10:运行服务器
![creating and using masterpages 26](https://i-blog.csdnimg.cn/blog_migrate/ac05b466b4ae9cf464e60d614b8c2c39.png)
![creating and using masterpages 27](https://i-blog.csdnimg.cn/blog_migrate/5c6a7b57868bf831a998aa3982c808d0.png)
Step 11: Open browser and type: http://127.0.0.1:4500
步骤11:打开浏览器并输入: http : //127.0.0.1 : 4500
![creating and using masterpages 28](https://i-blog.csdnimg.cn/blog_migrate/51d1149fac991f7b33317b45ccbee302.png)
翻译自: https://www.includehelp.com/Django/creating-and-using-masterpages.aspx