2009-08-23 15 views
0

テンプレートでget_absolute_urlを使用する際に問題があります。店のオブジェクトの1つを渡して{{ store.get_absolute_url }}と言うとうまくいくようですが、店の辞書を繰り返して、get_absolute_url関数を使用する必要がある場合は何も返しません。まさに私がやっていることは以下の通りです:Djangoの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

うわー、それはかなり複雑な質問でした。

あなたの問題はここにある:{% 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 }}は正常に動作します。

関連する問題