2012-12-31 3 views
0

get_absolute_url 메서드를 사용하여 내 URL에서 대체 될 슬러그 필드를 반환합니다. 반환되는 값이 비어있는 몇 가지 이유를 들어 Django get_absolute_url 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>  

위의 html에있는 c.get_absolute_url은 공백을 반환합니다.

+0

하자. 이'print category.objects.all() [0] .get_absolute_url()'을 할 수 있습니까? – rh0dium

+0

이것은 내가 얻은 오류입니다. TemplateSyntaxError at/ 렌더링하는 동안 NoReverseMatch가 발생했습니다 : 'catalog_category'에 '('category_slug ',)'인수가 있고 '{}'에 키워드 인수가 없습니다. – user1050619

+0

@Aamir 솔루션을 사용하고 있는지 확인하십시오. – rh0dium

답변

1

사용이 :

from django.core.urlresolvers import reverse 

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

행운은 없습니다. 같은 문제는 여전히 있습니다. – user1050619

0

데이터 입력을 할 때 당신이 슬러그를 설정하는 사용자를 요청하거나 프로그램 이름 필드에서 생성 된 슬러그가 있습니까?

도움말 텍스트에 나중에 나와 있지만 코드가 표시되지 않습니까? 당신은 저장 메서드를 재정의해야합니다의 첫 번째 역이 작동하는지 확인

How to create a unique slug in Django

관련 문제