我在试图找出如何提供“view_category”时遇到困难,这个视图可以输入到这个模板标签中(来自djangosnippets)。此外,对于此视图/标记,urls.py应该是什么样的?
来源(http://djangosnippets.org/snippets/475/#c4123):
from django.core.urlresolvers import reverse, NoReverseMatch
from django.template import Library, Variable, TemplateSyntaxError, Node
register = Library()
class RelatedNode(Node):
def __init__(self, object_list, viewname):
self.object_list = Variable(object_list)
self.viewname = viewname
def render(self, context):
def node(object):
try:
reversed = reverse(self.viewname, args=[object.id])
except NoReverseMatch:
return '<li>%s</li>' % object
return '<li><a href="%s">%s</li>' % (reversed, object)
def recursive(object):
if object.child_set.all():
output.append('<ul>')
for object in object.child_set.all():
output.append(node(object))
recursive(object)
output.append('</ul>')
output = []
for object in self.object_list.resolve(context):
if not object.parent:
output.append(node(object))
recursive(object)
return '\n'.join(output)
def related_linked_list(parser, token):
bits = token.contents.split()
if len(bits) != 3:
raise TemplateSyntaxError, "'%s' tag takes exactly 2 arguments" % bits[0]
return RelatedNode(bits[1], bits[2])
register.tag(related_linked_list)
答案 0 :(得分:0)
在你的网址:
url(r'^some-category/(\d+)/$', some_category_view, name="my-category-view")
对于您的代码,view_category ==&#34; my-category-view&#34;
......所以最终反过来说:
reversed = reverse("my-category-view", args=[object.id])