在Django中,放置html格式数据的简短片段的最佳位置是哪里?

时间:2022-08-16 11:23:49

This question is related to (but perhaps not quite the same as):

这个问题与(但可能与):

Does Django have HTML helpers?

Django有HTML助手吗?

My problem is this: In Django, I am constantly reproducing the basic formatting for low-level database objects. Here's an example:

我的问题是:在Django中,我一直在为底层数据库对象重新生成基本的格式。这里有一个例子:

I have two classes, Person and Address. There are multiple Addresses for each Person, setup likeso (in their respective models.py)

我有两个班,一个人,一个地址。每个人有多个地址,设置likeso(在各自的模型中)

class Person(models.Model):
  ...

class Address(models.Model):
  contact = models.ForeignKey(Person)

Now, whenever I look at a Person, I want to see all their Addresses. So suppose Persons/views.py has something likeso:

现在,每当我看到一个人,我都想看到他所有的地址。假设人/观点。py有likeso:

def detail(request, person_id):
   person = get_object_or_404( Person, pk=person_id )
   return render_to_response('persons/details.html',
      { 'title' : unicode(person), 'addresses': person.address_set.all() } )

And, I have a template, persons/details.html, with code, for example, like-so:

我有一个模板,人物/细节。html,带有代码,例如,like-so:

{% extends "base.html" %}

{% for address in addresses %}
<b>{{ address.name }}</b>
  {{ address.type }} <br>
  {{ address.street_1 }}<br>
  {{ address.street_2 }}<br>
  {{ address.city }} {{ address.stateprov }} {{ address.postalcode }}<br>
  {{ address.country }}
  <hr>
{{ endfor }}

I am repeating this code quite a bit, often with minor variations, such when it's in a table, and then < br > must be substituted by < /td >< td >. Other times, I don't want a street_2 to display (or the < br > after it). All to say, there is fundamental logic that I want to express, that I am even more loath to tote around with block-and-copy!

我重复了这段代码很多次,经常有一些细微的变化,比如在表中,然后< br >必须被< /td >< td >替换。有时候,我不希望显示street_2(或者后面的< br >)。总而言之,我想表达的基本逻辑是,我更不愿意到处玩弄权术!

What I want is a persons/details.html with, for example, the following:

我想要的是一个人/细节。例如,html包含以下内容:

{% extends "base.html" %}
{% for address in addresses %} 
  {% address.as_html4 %}
{% endfor %}

And if I want inline table, something likeso (I guess!):

如果我想要内联表,可以用类似于:

{% extends "base.html" %}
<table><tr>
{% for address in addresses %}
  <tr><td> {% address.as_html4 </td><td> %} </td></tr>
{% endfor %}
</table>

The question is, then: Where is the best place to put the formatting? The logic?

那么,问题是:放置格式的最佳位置在哪里?的逻辑吗?

Django seem to have the following (plausible) options:

Django似乎有以下(可能的)选项:

  1. Put the formatting in models.py

    将格式放在model .py中

  2. Put the logic/formatting in views.py

    将逻辑/格式放在views.y中

  3. Put the logic/formatting in some other sub-class of Person or Address (i.e. addresses/html4.py)

    将逻辑/格式放到其他的Person或Address子类中(例如Address /html4.py)

  4. Create custom tags

    创建自定义标签

Help / insight much appreciated!

非常感谢您的帮助/见解!

3 个解决方案

#1


13  

Sounds like an inclusion tag is what you're looking for. You could have a template and tag for each major variation and use the tag's arguments to customise the context for each template as required.

听起来好像包含标签就是你要找的。您可以为每个主要变体设置一个模板和标记,并使用标记的参数根据需要为每个模板定制上下文。

Basic tag definition:

基本标签定义:

@register.inclusion_tag('person/address.html')
def display_address(address):
    return {'address': address}

Use in templates (assuming the templatetag module containing it has already been {% load %}-ed):

在模板中使用(假设包含模板的templatetag模块已经被{% load %}-ed):

{% display_address address %}

#2


2  

I would use a template tag outputting data using a template html-file a k a inclusion-tag

我将使用模板标记输出数据,使用模板html-file a k a include -tag输出数据

#3


1  

I think template filter will be useful too. You can pass filter on each object, for example:

我认为模板过滤器也很有用。您可以在每个对象上传递过滤器,例如:

{{ value|linebreaks }} # standard django filter

Will produce:

会产生:

If value is Joel\nis a slug, the output will be <p>Joel<br>is a slug</p>.

See Django Built-in template tags and filters complete reference.

参见Django内置模板标签和过滤器完整的引用。

#1


13  

Sounds like an inclusion tag is what you're looking for. You could have a template and tag for each major variation and use the tag's arguments to customise the context for each template as required.

听起来好像包含标签就是你要找的。您可以为每个主要变体设置一个模板和标记,并使用标记的参数根据需要为每个模板定制上下文。

Basic tag definition:

基本标签定义:

@register.inclusion_tag('person/address.html')
def display_address(address):
    return {'address': address}

Use in templates (assuming the templatetag module containing it has already been {% load %}-ed):

在模板中使用(假设包含模板的templatetag模块已经被{% load %}-ed):

{% display_address address %}

#2


2  

I would use a template tag outputting data using a template html-file a k a inclusion-tag

我将使用模板标记输出数据,使用模板html-file a k a include -tag输出数据

#3


1  

I think template filter will be useful too. You can pass filter on each object, for example:

我认为模板过滤器也很有用。您可以在每个对象上传递过滤器,例如:

{{ value|linebreaks }} # standard django filter

Will produce:

会产生:

If value is Joel\nis a slug, the output will be <p>Joel<br>is a slug</p>.

See Django Built-in template tags and filters complete reference.

参见Django内置模板标签和过滤器完整的引用。