Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Database 如何在模板中使用Django链?_Database_Django_Templates - Fatal编程技术网

Database 如何在模板中使用Django链?

Database 如何在模板中使用Django链?,database,django,templates,Database,Django,Templates,我使用的链条如下所示: in views.py places_list = Place.objects.all().order_by('-datetimecreated')[:5] events_list = Event.objects.all().order_by('-datetimecreated')[:5] photos_list = Photo.objects.all().order_by('-datetimecreated')[:5] result_list = list(chain(

我使用的链条如下所示:

in views.py

places_list = Place.objects.all().order_by('-datetimecreated')[:5]
events_list = Event.objects.all().order_by('-datetimecreated')[:5]
photos_list = Photo.objects.all().order_by('-datetimecreated')[:5]
result_list = list(chain(photos_list, places_list, events_list))
在模板文件中

    {% for item in result_list %}

        <a href="{% url view_place item.slug %}">{{item.title}}</a>

    {% endfor %}
这是为了展示一个地方,但如何展示一个事件或照片

        <a href="{% url view_event item.slug %}">{{item.title}}</a>
        <a href="{% url view_photo item.slug %}">{{item.caption}}</a>

提前谢谢

为了正确回答你的问题,我需要知道更多,但这里有一些想法可能会对你有所帮助。我假设照片列表、地点列表和事件列表都是属于Django模型的不同类的对象

选项1:在每个类上定义一个确定对象类型的方法

例如,在每个模型上定义内容类型方法,如下所示:

class Photo(models.Model):
    def type(self):
        return 'photo'
class Photo(models.Model):
    ... # fields etc.

    @property
    def url(self):
        ... # return reversed url now

    @property
    def caption(self):
        ... # same idea, access title/whatnot
{% for item in item_list %}
    <a href="{{ item.url }}">{{ item.caption }}</a>
{% endfor %}
然后在模板中选中此项:

{% for item in result_list %}
    {% if item.type == "photo" %}
        ...
    {% elif item.type == "place" %}
        ...
    {% else %}
        ...
    {% end %}
{% endfor %}
{% for item in result_list %}
    {{ item.render }}
{% endfor %}
选项2:在每个类上定义渲染方法

这可能要难看得多,但您可以在每个对象上定义一个render方法,该方法返回要为该对象吐出的完整HTML。然后在模板中执行此操作:

{% for item in result_list %}
    {% if item.type == "photo" %}
        ...
    {% elif item.type == "place" %}
        ...
    {% else %}
        ...
    {% end %}
{% endfor %}
{% for item in result_list %}
    {{ item.render }}
{% endfor %}

旁注:考虑继承< /P>


听起来,照片、地点和事件都可能出现在同一个提要中,因此它们可能共享一些公共字段,如posted_at。关于这一点,你需要考虑的事情很多,所以最好看一下。

< P>我需要知道更多的问题来正确回答你的问题,但是这里有一些可能对你有帮助的想法。我假设照片列表、地点列表和事件列表都是属于Django模型的不同类的对象

选项1:在每个类上定义一个确定对象类型的方法

例如,在每个模型上定义内容类型方法,如下所示:

class Photo(models.Model):
    def type(self):
        return 'photo'
class Photo(models.Model):
    ... # fields etc.

    @property
    def url(self):
        ... # return reversed url now

    @property
    def caption(self):
        ... # same idea, access title/whatnot
{% for item in item_list %}
    <a href="{{ item.url }}">{{ item.caption }}</a>
{% endfor %}
然后在模板中选中此项:

{% for item in result_list %}
    {% if item.type == "photo" %}
        ...
    {% elif item.type == "place" %}
        ...
    {% else %}
        ...
    {% end %}
{% endfor %}
{% for item in result_list %}
    {{ item.render }}
{% endfor %}
选项2:在每个类上定义渲染方法

这可能要难看得多,但您可以在每个对象上定义一个render方法,该方法返回要为该对象吐出的完整HTML。然后在模板中执行此操作:

{% for item in result_list %}
    {% if item.type == "photo" %}
        ...
    {% elif item.type == "place" %}
        ...
    {% else %}
        ...
    {% end %}
{% endfor %}
{% for item in result_list %}
    {{ item.render }}
{% endfor %}

旁注:考虑继承< /P>


听起来,照片、地点和事件都可能出现在同一个提要中,因此它们可能共享一些公共字段,如posted_at。关于这一点你需要考虑的事情很多,所以最好看看.

你可以为你的每个模型定义一个代理属性,然后你可以在你的模板中使用它。考虑这样的事情:

class Photo(models.Model):
    def type(self):
        return 'photo'
class Photo(models.Model):
    ... # fields etc.

    @property
    def url(self):
        ... # return reversed url now

    @property
    def caption(self):
        ... # same idea, access title/whatnot
{% for item in item_list %}
    <a href="{{ item.url }}">{{ item.caption }}</a>
{% endfor %}
在模板中,它可能如下所示:

class Photo(models.Model):
    def type(self):
        return 'photo'
class Photo(models.Model):
    ... # fields etc.

    @property
    def url(self):
        ... # return reversed url now

    @property
    def caption(self):
        ... # same idea, access title/whatnot
{% for item in item_list %}
    <a href="{{ item.url }}">{{ item.caption }}</a>
{% endfor %}

您可以为每个模型定义一个代理属性,然后使用该属性在模板中创建代理。考虑这样的事情:

class Photo(models.Model):
    def type(self):
        return 'photo'
class Photo(models.Model):
    ... # fields etc.

    @property
    def url(self):
        ... # return reversed url now

    @property
    def caption(self):
        ... # same idea, access title/whatnot
{% for item in item_list %}
    <a href="{{ item.url }}">{{ item.caption }}</a>
{% endfor %}
在模板中,它可能如下所示:

class Photo(models.Model):
    def type(self):
        return 'photo'
class Photo(models.Model):
    ... # fields etc.

    @property
    def url(self):
        ... # return reversed url now

    @property
    def caption(self):
        ... # same idea, access title/whatnot
{% for item in item_list %}
    <a href="{{ item.url }}">{{ item.caption }}</a>
{% endfor %}

您好,谢谢您的重播,抱歉忘了提及照片、事件、地点是不同的型号。我的回答假设它们是不同的型号,所以这很好。它在生产服务器上不起作用:result\u list=listchainphotos\u list,places\u list,events\u list给出:list正好包含2个参数1!!!您是否覆盖了本地范围中的列表?检查您是否还没有编写另一个名为list anywhere的方法,或者没有将其包含在内。您好,感谢您的重播,抱歉忘了提及照片、事件、地点是不同的模型。我的回答假设它们是不同的模型,所以这没关系。它在生产服务器上不起作用:result\u list=listchainphotos\u list,places\u list,列表给出的事件:列表正好包含2个参数1!!!您是否覆盖了本地范围中的列表?请检查您是否在任何地方编写了另一个名为list的方法,或者没有包含它。谢谢您,我第一次尝试代理…:事实上,我认为你不需要让它们成为你的财产。模板引擎将调用该方法(如果是方法)。谢谢,我第一次尝试代理时…:事实上,我认为你不需要让它们成为你的财产。模板引擎将调用该方法(如果是方法)。