0

我在模板中使用 get_absolute_url 时遇到了一些问题。如果我只传入我的一个商店对象并说它似乎工作正常{{ store.get_absolute_url }},但如果我必须遍历商店字典然后使用该get_absolute_url函数,它什么也不返回。我正在做的事情如下:

  class Store(EthicalObject):
            type = "Store"
            name = models.CharField(max_length=50)
            company = models.ForeignKey(Company, verbose_name="Company", null=True, blank=True)
            location = models.OneToOneField(Location, verbose_name="Location", null=True, blank=True)
            products = models.ManyToManyField('Product', related_name="%(class)s_related", db_table=u'ethicsdb_products_to_stores', blank=True)
            companies = models.ManyToManyField('Company', related_name="%(class)s_related", db_table=u'ethicsdb_companies_to_stores', blank=True)

            def get_absolute_url(self):
                    return ('store_details', [str(self.id)])

            get_absolute_url = models.permalink(get_absolute_url)

这有效:

views.py:
def fetch_sidebar_data(shop_object):
        sidebar_modules = {}

        if shop_object.content_type.name == 'company':
                sidebar_modules['related_stores'] = shop_object.stores.all()
                sidebar_modules['related_products'] = shop_object.products.all()

        if shop_object.content_type.name == 'store':
                sidebar_modules['related_companies'] = shop_object.companies.all()
                sidebar_modules['related_products'] = shop_object.products.all()

        if shop_object.content_type.name == 'product':
                sidebar_modules['related_stores'] = shop_object.stores.all()
                sidebar_modules['related_companies'] = shop_object.companies.all()

        sidebar_modules['tags'] = shop_object.tags


        return sidebar_modules['related_stores'][1]

def company_details(request, company_id):
        company = get_object_or_404(Company, id=company_id)

        sidebar_modules = fetch_sidebar_data(company)

        return render_to_response('company/details.html', {'company': company, 'sidebar_modules': sidebar_modules}, context_instance=RequestContext(request))


template:

{% extends "base-onecol.html" %}

{% block page_div_extra_attr %}class="twocol"{% endblock %}

{% block sidebar_content %}
        <div id="sidebar-right">
        <h1>{{ sidebar_modules.name }}{{sidebar_modules.get_absolute_url }}</h1>
        </div>
{% endblock %}

这不起作用:

views.py:
def fetch_sidebar_data(shop_object):
        sidebar_modules = {}

        if shop_object.content_type.name == 'company':
                sidebar_modules['related_stores'] = shop_object.stores.all()
                sidebar_modules['related_products'] = shop_object.products.all()

    if shop_object.content_type.name == 'store':
            sidebar_modules['related_companies'] = shop_object.companies.all()
            sidebar_modules['related_products'] = shop_object.products.all()

    if shop_object.content_type.name == 'product':
            sidebar_modules['related_stores'] = shop_object.stores.all()
            sidebar_modules['related_companies'] = shop_object.companies.all()

    sidebar_modules['tags'] = shop_object.tags


    return sidebar_modules

template:

{% extends "base-onecol.html" %}

{% block page_div_extra_attr %}class="twocol"{% endblock %}

{% block sidebar_content %}
        <div id="sidebar-right">
        {% for module_name,module in sidebar_modules.items %}
                {% ifequal module_name "related_stores" %}
                        <h3>Sold Here</h3>
                        {% for related_store in module.values %}
                                <a href="{{ related_store.get_absolute_url }}">{{ related_store.name }}</a><br/>
                        {% endfor %}
                {% endifequal %}

                {% ifequal module_name "related_products" %}
                        <h3>Buy Local</h3>
                        {{ module }}<br/>
                {% endifequal %}

                {% ifequal module_name "related_companies" %}
                        <h3>
                        {{ module }}<br/>
                {% endifequal %}

                {% ifequal module_name "tags" %}
                        {{ module }}<br/>
                {% endifequal %}

        {% endfor %}
        </div>
{% endblock %}

在第二个中,我只是没有得到任何回报get_absolute_url。当我打印出来时,我知道它在其他地方工作。这是一个 Django 错误,无法get_absolute_url在字典中使用吗?

4

1 回答 1

4

哇,这是一个相当复杂的问题。

你的问题在这里:{% for related_store in module.values %}

module是一个查询集。.values正在调用 QuerySet 方法,该方法返回一个字典,其中包含每行的字段值。字典没有get_absolute_url属性,get_absolute_url也不是模型中的字段。

只需使用{% for related_store in module %},您将处理实际的模型实例而不是字典,这意味着{{ related_store.get_absolute_url }}可以正常工作。

于 2009-08-23T23:34:14.427 回答