Wednesday, July 25, 2012

Re: get_absolute_url returning empty string

Your get_absolute_url method is in the wrong format:::

it should be:
   @models.permalink
    def get_absolute_url(self):
         return ("view_name", #view name


On Tuesday, July 24, 2012 1:58:26 PM UTC-4, Jeff Green wrote:
I am stuck in trying to figure out why the get_absolute_url call in my template is giving me empty string.
 
Here is my models.py
 
class Category(models.Model):
    name = models.CharField(max_length=50)
    description = models.TextField()
    slug = models.SlugField(max_length=50, unique=True,
        help_text='Unique value for product page URL, created from name')
    description = models.TextField()
    is_active = models.BooleanField(default=True)
    meta_keywords = models.CharField("Meta keywords", max_length=255,
        help_text='Comma-delimited set of SEO keywords for meta tag')
    meta_description = models.CharField("Meta description", max_length=255,
        help_text='Content for meta description tag')
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    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 (show_category', [self.slug])
       
My urls.py
 
urlpatterns = patterns('catalog.views',
    url(r'^$', 'index', { 'template_name': 'catalog/index.html'}, 'catalog_home'),
    url(r'^category/(?P<category_slug>[-\w+])/$', 'show_category'),
    url(r'^product/(?P<product_slug>[-\w+])/$',
        'show_product',
        { 'template_name': 'catalog/product.html'}, 'catalog_product'),
)
 
My views.py
 
def show_category(request, category_slug, template_name='catalog/category.html'):
    c = get_object_or_404(Category, slug=category_slug)
    products = c.product_set.all()
    page_title = c.name
    meta_keywords = c.meta_keywords
    meta_description = c.meta_description
    return render_to_response(template_name, locals(),
        context_instance=RequestContext(request))
 
My template
 
<h3>Categories</h3>
{% for c in active_categories %}
    <a href="{{ c.get_absolute_url }}">{{ c.name }}</a><br/>
{% endfor %}
 
Any help would be greatly appreciated. Thanks.
 
Jeff

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/QapYPqc7mFIJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment