简体   繁体   English

Django 中的模板太多

[英]Too many templates in Django

I am learning Django and I programming a CRUD for various objects.我正在学习 Django,并为各种对象编写 CRUD。 Currently I need to generate three templates per object: one for creation, one for update and one for a list.目前我需要为每个对象生成三个模板:一个用于创建,一个用于更新,一个用于列表。 Look at how many templates I got in the picture.看看我在图片中得到了多少模板。

The process is becoming painful.这个过程越来越痛苦。

What I am doing wrong and how to correct it?我做错了什么以及如何纠正它?

Thanks.谢谢。

模板

From your comment:从你的评论:

The problem is that If I need to make a change, for example, add a new button, I have to make it in all the templates.问题是如果我需要进行更改,例如添加一个新按钮,我必须在所有模板中进行更改。

That sounds wrong.这听起来不对。 Either a button belong to one type of thing and then it should only exist in that template, or it should be on every page and then it should be in the base template .一个按钮要么属于一种类型的东西,那么它应该只存在于该模板中,或者它应该在每个页面上,然后它应该在基础模板中

You can make one template with your basic page layout (including such buttons), and have "blocks" in them that are then filled out by each template that extends the base template.您可以使用基本页面布局(包括此类按钮)制作一个模板,并在其中包含“块”,然后由扩展基本模板的每个模板填充。

See the Django documentation about template inheritance .请参阅有关模板继承的 Django 文档

Also, I think that in most cases the "create" and "update" pages are going to be extremely similar;此外,我认为在大多数情况下,“创建”和“更新”页面将非常相似; usually they use the same template, one with data already filled in, the other without.通常他们使用相同的模板,一个已经填写了数据,另一个没有。

You could have a base template, then a generic list template and a generic edit/create template, with templates for each type of thing extending the list and the edit templates to fill in only the relevant fields.您可以有一个基本模板,然后是一个通用列表模板和一个通用编辑/创建模板,每种类型的模板都有扩展列表和编辑模板以仅填充相关字段。

Also, maybe you use HTML that is close enough to what the Django forms can render themselves.此外,也许您使用的 HTML 与 Django 表单可以呈现的内容非常接近。 Then as long as you call the edit/create form the same in each of your views (like "form"), the template can just render the form and they can all use the same template.然后只要您在每个视图中调用相同的编辑/创建表单(如“表单”),模板就可以渲染表单并且它们都可以使用相同的模板。

If you're building a big web-app then this is normal for templates to be more.

Heavy (big) web-app means = more templates + more code + more time + more features

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM