【问题标题】:Obtain the first part of an URL from Django template从 Django 模板获取 URL 的第一部分
【发布时间】:2011-07-18 04:05:28
【问题描述】:

我使用request.path 来获取当前的URL。例如,如果当前 URL 是“/test/foo/baz”,我想知道它是否以字符串序列开头,比如说 /test。如果我尝试使用:

{% if request.path.startswith('/test') %}
    Test
{% endif %} 

我收到一条错误消息,提示它无法解析表达式的其余部分:

Could not parse the remainder: '('/test')' from 'request.path.startswith('/test')'
Request Method: GET
Request URL:    http://localhost:8021/test/foo/baz/
Exception Type: TemplateSyntaxError
Exception Value:    
Could not parse the remainder: '('/test')' from 'request.path.startswith('/test')'
Exception Location: C:\Python25\lib\site-packages\django\template\__init__.py in   __init__, line 528
Python Executable:  C:\Python25\python.exe
Python Version: 2.5.4
Template error

一种解决方案是创建一个自定义标签来完成这项工作。还有其他东西可以解决我的问题吗?使用的 Django 版本是 1.0.4。

【问题讨论】:

    标签: python django templates url django-templates


    【解决方案1】:

    您可以使用切片过滤器获取url的第一部分

    {% if request.path|slice:":5" == '/test' %}
        Test
    {% endif %} 
    

    现在不能尝试这个,也不知道过滤器是否在'if'标签内工作, 如果不起作用,您可以使用'with'标签

    {% with request.path|slice:":5" as path %}
      {% if path == '/test' %}
        Test
      {% endif %} 
    {% endwith %} 
    

    【讨论】:

      【解决方案2】:

      您可以通过使用内置 in 标记检查成员资格来获得相同的结果,而不是检查带有开头的前缀。

      {% if '/test' in request.path %}
          Test
      {% endif %} 
      

      这将通过字符串不是严格开头的情况,但您可以简单地避免这些类型的 URL。

      【讨论】:

      • 这就是我想要的。谢谢!绝对是这里的最佳答案。
      • 如果我们需要查找我们是否在站点的某个部分,则在某些情况下将不起作用。例如,我们需要发现我们在“课程”部分。 /courses/1/ - 好的,/login/?next=/courses/1/ - 不好。
      【解决方案3】:

      您不能从 django 模板中将参数传递给普通的 python 函数。为了解决您的问题,您需要一个自定义模板标签:http://djangosnippets.org/snippets/806/

      【讨论】:

        【解决方案4】:

        按照设计,您不能使用 Django 模板中的参数调用函数。

        一种简单的方法是将您需要的状态放入请求上下文中,如下所示:

        def index(request):
            c = {'is_test' : request.path.startswith('/test')}
            return render_to_response('index.html', c, context_instance=RequestContext(request))
        

        然后您将拥有一个可在模板中使用的is_test 变量:

        {% if is_test %}
            Test
        {% endif %}
        

        这种方法还具有从模板中抽象出确切路径测试('/test')的优点,这可能会有所帮助。

        【讨论】:

          【解决方案5】:

          来自this page of Django docs的哲学部分:

          模板系统不会执行 任意 Python 表达式

          如果路径以'/test'开头,你真的应该写一个自定义标签或传递一个变量来通知模板

          【讨论】:

            【解决方案6】:

            在这种情况下我使用上下文处理器:

            *。创建文件 core/context_processors.py :

            def variables(request):
                    url_parts = request.path.split('/')
                    return {
                        'url_part_1': url_parts[1],
                    }
            

            *。添加记录:

            'core.context_processors.variables',
            

            在 settings.py 到 TEMPLATES 'context_processors' 列表中。

            *。使用

            {{ url_part_1 }}
            

            在任何模板中。

            【讨论】:

              猜你喜欢
              • 2021-10-09
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2017-11-12
              • 2023-03-23
              • 1970-01-01
              • 2018-12-04
              相关资源
              最近更新 更多