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은 공백을 반환합니다.
하자. 이'print category.objects.all() [0] .get_absolute_url()'을 할 수 있습니까? – rh0dium
이것은 내가 얻은 오류입니다. TemplateSyntaxError at/ 렌더링하는 동안 NoReverseMatch가 발생했습니다 : 'catalog_category'에 '('category_slug ',)'인수가 있고 '{}'에 키워드 인수가 없습니다. – user1050619
@Aamir 솔루션을 사용하고 있는지 확인하십시오. – rh0dium