views:

406

answers:

1

I'm having some trouble using get_absolute_url in a template. It seems to work fine if I just pass in one of my store objects and say {{ store.get_absolute_url }}, but if I have to iterate through a dictionary of stores and then use the get_absolute_url function, it returns nothing. Exactly what I'm doing is below:

  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)

This works:

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 %}

This doesn't work:

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 %}

In the second one, I just get no return from get_absolute_url. I know it's working in other places when I print it out. Is this a Django bug, the inability to use get_absolute_url in a dictionary of dictionaries?

+4  A: 

Wow, that was a rather convoluted question.

Your problem is here: {% for related_store in module.values %}

module is a QuerySet. .values is calling the QuerySet method which returns a dictionary containing the field values for each row. A dictionary has no get_absolute_url attribute, and get_absolute_url isn't a field in the model.

Just use {% for related_store in module %} and you'll be dealing with actual model instances rather than dictionaries, which means {{ related_store.get_absolute_url }} will work fine.

SmileyChris