user1050619
user1050619

Reputation: 20906

Django get_absolute_url blank

Im using get_absolute_url method to return a slug field which will be substituted in my url.

For some reason, the value that is returned is blank,

models.py

class Category(models.Model):
    """ model class containing information about a category in the product catalog """
    name = models.CharField(max_length=50)
    slug = models.SlugField(max_length=50, unique=True,
                            help_text='Unique value for product page URL, created automatically from name.')
    description = models.TextField()
    is_active = models.BooleanField(default=True)
    meta_keywords = models.CharField(max_length=255,
                                     help_text='Comma-delimited set of SEO keywords for keywords meta tag')
    meta_description = models.CharField(max_length=255,
                                        help_text='Content for description meta tag')
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    #objects = models.Manager()
    #active = ActiveCategoryManager()

    class Meta:
        db_table = 'categories'
        ordering = ['name']
        verbose_name_plural = 'Categories'

    def __unicode__(self):
        return self.name

    @models.permalink
    def get_absolute_url(self):
        return ('catalog_category', (), { 'category_slug': self.slug })

urls.py

urlpatterns = patterns('catalog.views', 
        (r'^$', 'index', { 'template_name':'catalog/index.html'}, 'catalog_home'), 
        (r'^category/(?P<category_slug>[-\w]+)/$',   
            'show_category', { 'template_name':'catalog/category.html'},'catalog_category'),  
        (r'^product/(?P<product_slug>[-\w]+)/$',  
            'show_product', { 'template_name':'catalog/product.html'},'catalog_product'), 

) 

catalog_tags.py

@register.inclusion_tag("tags/category_list.html") 
def category_list(request_path): 
    print 'catalog_tags-request_path', request_path
    #active_categories = Category.objects.filter(is_active=True) 
    active_categories = Category.objects.all() 
    return { 
          'active_categories': active_categories, 
          'request_path': request_path 
     } 

catalog_list.html

<h3>Categories</h3> 
<ul id="categories">
{% with active_categories as cats %} 
    {% for c in cats %} 

    <li>
     {% ifequal c.get_absolute_url request_path %} 
          {{ c}}<br /> 
     {% else %} 
          <a href="{{ c.get_absolute_url }}" class="category">{{ c.name }}</a><br /> 
     {% endifequal %} 
     </li>
    {% endfor %} 
{% endwith %} 
</ul>       

The c.get_absolute_url in the above html returns blank.

Upvotes: 0

Views: 942

Answers (2)

datakid
datakid

Reputation: 2712

Do you ask the user to set the slug when doing data entry or is the slug programmatically created from the name field?

The help text suggests the later, but I don't see the code? You need to override the save method:

How to create a unique slug in Django

Upvotes: 0

Aamir Rind
Aamir Rind

Reputation: 39699

Use this:

from django.core.urlresolvers import reverse

def get_absolute_url(self):
    return reverse('catalog_category', kwargs={'category_slug': self.slug})

Upvotes: 1

Related Questions