【问题标题】:breadcrumb_url - Reverse for '' not found. '' is not a valid view function or pattern namebreadcrumb_url - 未找到“”的反向。 '' 不是有效的视图函数或模式名称
【发布时间】:2019-11-04 22:50:07
【问题描述】:

我正在尝试通过这个例子来做面包屑https://www.djangosnippets.org/snippets/1289/但是有错误Reverse for '' not found. '' is not a valid view function or pattern name.我做了一切就像在这个例子中一样。但我做错了。

这行错误{% breadcrumb_url 'Home' product_list %}

urls.py

app_name = 'shop'
urlpatterns = [
    url(r'^$', views.product_list, name='product_list'),
    url(r'^show/(?P<slug>[-\w]+)$', views.product_show, name='product_show'),
    url(r'^(?P<category>[-\w]+)$', views.product_list, name='lst_by_ctgry'),
    url(r'^(?P<category>[-\w]+)/(?P<subcategory>[-\w]+)$', views.product_list, name='lst_by_subctgry'),
    url(r'^(?P<category>[-\w]+)/(?P<subcategory>[-\w]+)/(?P<kind>[-\w]+)$', views.product_list, name='lst_by_knds'),
]

base.html

{% load breadcrumbs %}
<!DOCTYPE html>
<html>
    <head>
        <title>Shop</title>
        <meta charset="utf-8">
    </head>

    <body>
        {% block breadcrumbs %}
            {% breadcrumb_url 'Home' product_list %}
        {% endblock %}

        <div class="container">
            {% block content %} {% endblock %}
        </div>
    </body>
</html>

面包屑.py

from django import template
from django.template import loader, Node, Variable
from django.utils.encoding import smart_str, smart_text
from django.template.defaulttags import url
from django.template import VariableDoesNotExist

register = template.Library()

@register.tag
def breadcrumb(parser, token):
    """
    Renders the breadcrumb.
    Examples:
        {% breadcrumb "Title of breadcrumb" url_var %}
        {% breadcrumb context_var  url_var %}
        {% breadcrumb "Just the title" %}
        {% breadcrumb just_context_var %}

    Parameters:
    -First parameter is the title of the crumb,
    -Second (optional) parameter is the url variable to link to, produced by url tag, i.e.:
        {% url person_detail object.id as person_url %}
        then:
        {% breadcrumb person.name person_url %}

    @author Andriy Drozdyuk
    """
    return BreadcrumbNode(token.split_contents()[1:])


@register.tag
def breadcrumb_url(parser, token):
    """
    Same as breadcrumb
    but instead of url context variable takes in all the
    arguments URL tag takes.
        {% breadcrumb "Title of breadcrumb" person_detail person.id %}
        {% breadcrumb person.name person_detail person.id %}
    """

    bits = token.split_contents()
    if len(bits)==2:
        return breadcrumb(parser, token)

    # Extract our extra title parameter
    title = bits.pop(1)
    token.contents = ' '.join(bits)

    url_node = url(parser, token)

    return UrlBreadcrumbNode(title, url_node)


class BreadcrumbNode(Node):
    def __init__(self, vars):
        """
        First var is title, second var is url context variable
        """
        self.vars = map(Variable,vars)

    def render(self, context):
        title = self.vars[0].var

        if title.find("'")==-1 and title.find('"')==-1:
            try:
                val = self.vars[0]
                title = val.resolve(context)
            except:
                title = ''

        else:
            title=title.strip("'").strip('"')
            title=smart_text(title)

        url = None

        if len(self.vars)>1:
            val = self.vars[1]
            try:
                url = val.resolve(context)
            except VariableDoesNotExist:
                print('URL does not exist', val)
                url = None

        return create_crumb(title, url)


class UrlBreadcrumbNode(Node):
    def __init__(self, title, url_node):
        self.title = Variable(title)
        self.url_node = url_node

    def render(self, context):
        title = self.title.var

        if title.find("'")==-1 and title.find('"')==-1:
            try:
                val = self.title
                title = val.resolve(context)
            except:
                title = ''
        else:
            title=title.strip("'").strip('"')
            title=smart_text(title)

        url = self.url_node.render(context)
        return create_crumb(title, url)


def create_crumb(title, url=None):
    """
    Helper function
    """
    crumb = """<span class="breadcrumbs-arrow">""" \
            """<img src="#" alt="Arrow">""" \
            """</span>"""
    if url:
        crumb = "%s<a href='%s'>%s</a>" % (crumb, url, title)
    else:
        crumb = "%s&nbsp;&nbsp;%s" % (crumb, title)

    return crumb

请帮帮我。谢谢!

【问题讨论】:

    标签: django django-templates


    【解决方案1】:

    当您指定{% breadcrumb_url 'Home' product_list %} 时,它假定product_list 是一个变量。相反,您想用引号括起来表示它是 URL 的名称

    breadcrumb_url 采用标题后跟与 url 相同的参数。所以既然你会这样做

    {% url 'shop:product_list' %}
    

    面包屑网址变成

    {% breadcrumb_url 'Home' 'shop:product_list' %}
    

    【讨论】:

    • {% breadcrumb_url 'Home' 'shop:product_list' %} 为我工作,谢谢!)
    猜你喜欢
    • 2018-11-19
    • 2019-04-06
    • 2019-08-13
    • 2021-04-21
    • 2018-05-20
    • 2018-07-01
    • 2021-11-06
    • 2023-04-10
    • 2019-05-19
    相关资源
    最近更新 更多