How to use Python to develop the template management function of a CMS system
With the rapid development of the Internet, content management systems (CMS) have attracted more and more attention. The CMS system can help users manage website content conveniently, and also provides developers with a way to develop quickly. This article will introduce how to use Python to develop the template management function of the CMS system to achieve flexible management and display of website content.
Suppose we want to create a template with navigation bar, sidebar and content area, we can create a file named base.html in the template folder and then write in the file The following code:
<!DOCTYPE html> <html> <head> <title>{% block title %}{% endblock %}</title> </head> <body> <header> <!-- 导航栏 --> <nav> <ul> <li><a href="#">首页</a></li> <li><a href="#">关于我们</a></li> <li><a href="#">联系我们</a></li> </ul> </nav> </header> <aside> <!-- 侧边栏 --> <ul> <li><a href="#">最新文章</a></li> <li><a href="#">热门文章</a></li> <li><a href="#">推荐文章</a></li> </ul> </aside> <main> <!-- 内容区域 --> {% block content %} {% endblock %} </main> <footer> <!-- 底部信息 --> <p>版权所有 © 2022</p> </footer> </body> </html>
from django.db import models class Template(models.Model): name = models.CharField(max_length=100) file = models.FileField(upload_to='templates/') def __str__(self): return self.name
In the model, we define a class named Template, which has two fields: name and file. The name field is used to store the name of the template, and the file field is used to store the path of the template file. By overriding the __str__ method, we can display the name of the template in the background management interface.
from django.urls import path from .views import TemplateListView, TemplateCreateView, TemplateUpdateView, TemplateDeleteView urlpatterns = [ path('templates/', TemplateListView.as_view(), name='template_list'), path('templates/create/', TemplateCreateView.as_view(), name='template_create'), path('templates/update/<int:pk>/', TemplateUpdateView.as_view(), name='template_update'), path('templates/delete/<int:pk>/', TemplateDeleteView.as_view(), name='template_delete'), ]
In the code, we define four URL addresses and associate them with the corresponding view functions. Among them, TemplateListView is used to display the template list, TemplateCreateView is used to create templates, TemplateUpdateView is used to update templates, and TemplateDeleteView is used to delete templates.
from django.views.generic import ListView, CreateView, UpdateView, DeleteView from .models import Template class TemplateListView(ListView): model = Template template_name = 'template_list.html' class TemplateCreateView(CreateView): model = Template template_name = 'template_create.html' fields = '__all__' class TemplateUpdateView(UpdateView): model = Template template_name = 'template_update.html' fields = '__all__' class TemplateDeleteView(DeleteView): model = Template template_name = 'template_delete.html' success_url = '/templates/'
In the code, we use Django's class views, which can simplify the writing of view functions. For TemplateListView, we specified the model as Template and set the template name to template_list.html. For TemplateCreateView, TemplateUpdateView and TemplateDeleteView, we specified the corresponding model, template name and fields respectively.
template_list.html:
<!DOCTYPE html> <html> <head> <title>模板列表</title> </head> <body> <h1>模板列表</h1> <table> <tr> <th>ID</th> <th>名称</th> <th>文件</th> <th>操作</th> </tr> {% for template in object_list %} <tr> <td>{{ template.id }}</td> <td>{{ template.name }}</td> <td>{{ template.file }}</td> <td> <a href="{% url 'template_update' template.id %}">编辑</a> <a href="{% url 'template_delete' template.id %}">删除</a> </td> </tr> {% endfor %} </table> <a href="{% url 'template_create' %}">创建模板</a> </body> </html>
template_create.html:
<!DOCTYPE html> <html> <head> <title>创建模板</title> </head> <body> <h1>创建模板</h1> <form method="post"> {% csrf_token %} <input type="text" name="name" placeholder="名称"><br> <input type="file" name="file"><br> <input type="submit" value="提交"> </form> </body> </html>
template_update.html:
<!DOCTYPE html> <html> <head> <title>更新模板</title> </head> <body> <h1>更新模板</h1> <form method="post"> {% csrf_token %} <input type="text" name="name" value="{{ object.name }}"><br> <input type="file" name="file" value="{{ object.file }}"><br> <input type="submit" value="提交"> </form> </body> </html>
template_delete.html:
<!DOCTYPE html> <html> <head> <title>删除模板</title> </head> <body> <h1>删除模板</h1> <p>确定要删除模板 "{{ object.name }}" 吗?</p> <form method="post"> {% csrf_token %} <input type="submit" value="确定"> <a href="{% url 'template_list' %}">取消</a> </form> </body> </html>
So far, we have completed the development of the template management function of the CMS system using Python. Through the above steps, we can easily create, update and delete templates, and display the content of the template in the front-end page.
Summary
This article introduces how to use Python to develop the template management function of the CMS system to achieve flexible management and display of website content. By creating templates, configuring routing, creating views and template pages in Django, we can easily create, update and delete templates. Through the above steps, we can better manage the content of the website, improve user experience, and also provide developers with a way to develop quickly.
The above is the detailed content of How to use Python to develop the template management function of CMS system. For more information, please follow other related articles on the PHP Chinese website!