Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/317.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
Python 在Django中呈现模板变量时出现问题_Python_Django_Django Templates - Fatal编程技术网

Python 在Django中呈现模板变量时出现问题

Python 在Django中呈现模板变量时出现问题,python,django,django-templates,Python,Django,Django Templates,说到解决这个问题,我真是碰壁了。我有一个包含在其他模板中的模板,但无法在包含的模板中呈现任何模板变量。对于包含的模板,我有一个单独的模板标记文件。我现在完全不知道如何解决这个问题 我希望能够在模板中呈现我的模型中的字段值(包括图像、简短描述等)。我相当确定我的做法是错误的 下面是我的代码: 模型: class AddOnItem(models.Model): base_product = models.ForeignKey(Product) base_price = models

说到解决这个问题,我真是碰壁了。我有一个包含在其他模板中的模板,但无法在包含的模板中呈现任何模板变量。对于包含的模板,我有一个单独的模板标记文件。我现在完全不知道如何解决这个问题

我希望能够在模板中呈现我的模型中的字段值(包括图像、简短描述等)。我相当确定我的做法是错误的

下面是我的代码:

模型:

class AddOnItem(models.Model):
    base_product = models.ForeignKey(Product)
    base_price = models.DecimalField(max_digits=8, decimal_places=2, default=0.0)
    addon_image = models.ImageField(upload_to='uploads/shop/product/images/', 
            default='uploads/shop/product/images/', help_text='Max width: 450px')
    short_description = models.CharField(max_length=255, blank=True)

    def __unicode__(self):
        return self.base_product.name
模板:

{% load addon_tags %}
{% if items_to_add %}
  {% for items in items_to_add %}
  <div id="addon-container">
    <div id="left-addon" class="addon-container">
        <a href=""><img src="#" class="addon-image"></a>
        <p class="addon-description">{{items.short_description}}</p>
    </div>
  </div>
  {% endfor %}
 {% endif %}
我想我要么做错了很多(我目前的假设),要么遗漏了一些小的东西。我已经在这方面工作了好几天,并且反复检查了文档。我只是完全错过了一些东西,这已经成为我的主要障碍。任何帮助都将不胜感激

Django版本1.4

编辑: 我最终重写了视图并删除了templatetag。感谢Daniel Roseman和Odif Yltsaeb的回复。

1)从您的帖子中,您正在将空的新项目添加到渲染模板标签的上下文中

2) 我在你的帖子中看不到你正在使用{%add_on_render%}templatetag。您已经创建了templattag,但似乎没有在任何地方使用它

很难理解您到底想做什么,或者为什么需要templatetag

如果你想显示models字段值,你不需要templateag,而你在这篇文章的“模板”部分展示的所有东西,都可以在你的主模板中很好地显示出来,我想,它不会在你的文章中显示出来

如果要使用templatetag,则此templatetag可能会接收AddOnItem istance作为参数,如下所示:

@register.inclusion_tag("foo/templates/v/sho/addon.html", takes_context=True)
def add_on_render(item):
    context['items_to_add'] = item

    return context
{% load addon_tags %}
{% if items_to_add %}
  {% for items in items_to_add %}
  <div id="addon-container">
    <div id="left-addon" class="addon-container">
        <a href=""><img src="#" class="addon-image"></a>
        <p class="addon-description">{% add_on_render items %}</p>
    </div>
  </div>
  {% endfor %}
 {% endif %}
{{ items_to_add.short_description }}
{% add_on_render my_product %}
您可以在以下模板中使用它:

@register.inclusion_tag("foo/templates/v/sho/addon.html", takes_context=True)
def add_on_render(item):
    context['items_to_add'] = item

    return context
{% load addon_tags %}
{% if items_to_add %}
  {% for items in items_to_add %}
  <div id="addon-container">
    <div id="left-addon" class="addon-container">
        <a href=""><img src="#" class="addon-image"></a>
        <p class="addon-description">{% add_on_render items %}</p>
    </div>
  </div>
  {% endfor %}
 {% endif %}
{{ items_to_add.short_description }}
{% add_on_render my_product %}

但是这样做似乎非常不必要,因为如果不使用templatag,您可以通过使用“主”模板之外已有的模板代码来实现所有这些。

您还没有发布您试图包含标记的模板。我怀疑您根本没有调用它,因为如果您尝试使用该标记,会有一些错误导致异常。您需要在主模板的某个地方执行
{%add\u on\u render%}

正如我所说,虽然有几个错误。首先,在将
项添加到\u add
键之前,不要定义
上下文(作为空dict)。你可以通过一次完成来缩短这个过程

其次,您将
项添加为一个单一的空白添加项。因此,在包含的模板中,迭代
项以添加
根本不起任何作用。不知道你在那里想做什么。也许您想传递所有AddOnItem实例

context = {'items_to_add':  AddOnItem.objects.all()}
或者,您可能希望通过某些条件对它们进行过滤,在这种情况下,您可能希望将这些条件传递给包含标记本身:

def add_on_render(product):
    context = {'items_to_add':  AddOnItem.objects.filter(base_product=product)}
您可以从主模板调用它,如下所示:

@register.inclusion_tag("foo/templates/v/sho/addon.html", takes_context=True)
def add_on_render(item):
    context['items_to_add'] = item

    return context
{% load addon_tags %}
{% if items_to_add %}
  {% for items in items_to_add %}
  <div id="addon-container">
    <div id="left-addon" class="addon-container">
        <a href=""><img src="#" class="addon-image"></a>
        <p class="addon-description">{% add_on_render items %}</p>
    </div>
  </div>
  {% endfor %}
 {% endif %}
{{ items_to_add.short_description }}
{% add_on_render my_product %}
如果设置“takes_context=True”,则应将context作为修饰函数中的第一个参数:

@register.inclusion_tag("foo/templates/v/sho/addon.html", takes_context=True)
def add_on_render(context):
    context['items_to_add'] = AddOnItem()
    ....