为什么Django的DetailView没有可重用的模板? [英] Why is not there a reusable template for Django's DetailView?

查看:112
本文介绍了为什么Django的DetailView没有可重用的模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在模板中显示表单在Django中相当容易:

 < form action =method =post > {%csrf_token%} 
{{form}}
< input type =submitvalue =Update/>
< / form>

基本上只是一个字 - 显示 {{form}} 。它是如此简单,您可以使用相同的模板为不同的表单。



您可以使用字段限制要在表单上显示的字段= [] 列表,如果您使用CBV的,如CreateView或UpdateView。



与此平行绘制,一个期望有类似的工作流程显示模型(而不是编辑),如在DetailView中。但是,没有这样的东西..你必须为每个使用的DetailView编写一个自定义模板。例如:

 < h3>用户:{{user}}< / h3> 
< label>名字< / label> ;: {{user.first_name}}< br />
< label>姓氏< / label> ;: {{user.last_name}}< br />
< label>用户名< / label> ;: {{user.username}}< br />
< label> School< / label> ;: {{user.person.school.name}}< br />

这与 {{form}} 将生成,除了这里打印的字段值,而不是在那里打印输入



所以,我想知道,为什么DetailView没有可重用的通用模板?有没有技术上的限制,还是只是不能像我想像的那么可重复使用?

解决方案

我认为这不是



可能会有可能定义标准的方式来渲染简单的模型属性,如 CharField - 当您进入更复杂的关系领域(如 ManyToManyField ForeignKey OneToOneField 。除了最简单的模型之外,您将最终超越任何默认表示。



其次,Django对于您的模型是不是也不应该 - ,因此它并不意味着它不会假设你想如何渲染它们。



这与形式定义的不同,其中单个表单域的结构被定义在Django和HTML中,两者之间有很强的相关性。


Displaying forms in a template is rather easy in Django:

<form action="" method="post">{% csrf_token %}
    {{ form }}
    <input type="submit" value="Update" />
</form>

It is basically just one word - display the {{ form }}. It is so simple that you can use the same template for different forms.

You can limit the fields to be shown on the form using the fields = [] list if you are using CBV's such as CreateView or UpdateView.

Drawing parallel to this, one expects to have a similar workflow for showing the models as well (as opposed to editing) such as in DetailView. But, there is no such thing.. You have to write a custom template for every DetailView that you use. Such as:

<h3>User: {{ user }}</h3>
<label>First Name</label>: {{ user.first_name }} <br />
<label>Last Name</label>: {{ user.last_name }} <br />
<label>Username</label>: {{ user.username }} <br />
<label>School</label>: {{ user.person.school.name }} <br />

This is very similar to what the {{ form }} would generate, except for the field values printed here, as opposed toinputs being printed there.

So, I wonder, why isn't there a reusable generic template for DetailView's? Is there a technical limitation for this, or is it just not as reusable as I imagine?

解决方案

I think it is not as reusable as you imagine.

It might conceivably be possible to define "standard" ways to render simple model properties like CharField - this quickly becomes impossible when you get into more complex relational fields like ManyToManyField, ForeignKey, OneToOneField. You would end up overriding any default representation very quickly for anything but the simplest of models.

Secondly Django is not - and should not be - opinionated about what your models are for, and therefore it makes sense that it doesn't try to assume how you want to render them.

This is different from forms where the structure of individual form fields is defined in Django and in HTML, and there is a strong correlation between the two.

这篇关于为什么Django的DetailView没有可重用的模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆