0

テンプレートで get_absolute_url を使用すると問題が発生します。ストア オブジェクトの 1 つを渡して と言うと問題なく動作するように見えますが、ストア{{ 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 %}

2 つ目では、 から何も返されません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 に答える