无法解析余下的部分:’'|’ 从 ‘ ‘中’' 在Django模板分页时

huangapple go评论176阅读模式
英文:

Could not parse the remainder: '|' from '|' in Django template while paginating

问题

在分页博客应用程序时,出现了以下错误。
TemplateSyntaxError at /home/
无法解析剩余部分:'|' 从 '|'

请求方法:GET
请求URL:http://127.0.0.1:8000/home/?page1
Django 版本:4.2.2
异常类型:TemplateSyntaxError
异常值:
无法解析剩余部分:'|' 从 '|'
异常位置:C:\Users\chand\venv\Lib\site-packages\django\template\base.py,第703行,__init__内
触发位置:blog.views.PostListView
Python 可执行文件:C:\Users\chand\venv\Scripts\python.exe
Python 版本:3.11.3
Python 路径:
['C:\Users\chand\django_project',
'C:\Users\chand\AppData\Local\Programs\Python\Python311\python311.zip',
'C:\Users\chand\AppData\Local\Programs\Python\Python311\DLLs',
'C:\Users\chand\AppData\Local\Programs\Python\Python311\Lib',
'C:\Users\chand\AppData\Local\Programs\Python\Python311',
'C:\Users\chand\venv',
'C:\Users\chand\venv\Lib\site-packages']
服务器时间:周一,2023年7月10日 10:24:46 +0000

这是我编写的 HTML 模板:

{% extends "blog/base.html" %}
{% block content %}
    {% for post in posts %}
        <article class="media content-section">
          <img class="rounded-circle article-img" src="{{ post.author.profile.image.url }}">
          <div class="media-body">
            <div class="article-metadata">
              <a class="mr-2" href="#">{{ post.author }}</a>
              <small class="text-muted">{{ post.date_posted|date:"F d, Y" }}</small>
            </div>
            <h2><a class="article-title" href="{% url 'post_detail' post.id %}">{{ post.title }}</a></h2>
            <p class="article-content">{{ post.content }}</p>
          </div>
        </article>
    {% endfor %}

    {% if is_paginated %}
        {% if page_obj.has_previous %}
          <a class="btn btn-outline-info mb-4" href="?page=1">First</a>
          <a class="btn btn-outline-info mb-4" href="{{ page_obj.previous_page_number }}">Previous</a>
        {% endif %}

        {% for num in page_obj.paginator.page_range %}
          {% if page_obj.number == num %}
            <a class="btn btn-info mb-4" href="?page={{ num }}">{{num}}</a>   
          {% elif num > page_obj.number | add:'-3' and num < page.obj.number | add:'3' %}  
            <a class="btn btn-outline-info mb-4" href="?page={{ num }}">{{num}}</a>   
          {% endif %}
        {% endfor %}

          {% if page_obj.has_next %}
            <a class="btn btn-outline-info mb-4" href="{{ page_obj.next_page_number}}">Next</a>
            <a class="btn btn-outline-info mb-4" href="{{ page_obj.paginator.num_pages }}">Last</a>
          {% endif %}
    {% endif %}
{% endblock content %}

有人能帮我解决这个模板错误吗?

英文:

while paginating the blog app , the following error showed up.
TemplateSyntaxError at /home/
Could not parse the remainder: '|' from '|'

Request Method: GET
Request URL: http://127.0.0.1:8000/home/?page1
Django Version: 4.2.2
Exception Type: TemplateSyntaxError
Exception Value:
Could not parse the remainder: '|' from '|'
Exception Location: C:\Users\chand\venv\Lib\site-packages\django\template\base.py, line 703, in init
Raised during: blog.views.PostListView
Python Executable: C:\Users\chand\venv\Scripts\python.exe
Python Version: 3.11.3
Python Path:
['C:\Users\chand\django_project',
'C:\Users\chand\AppData\Local\Programs\Python\Python311\python311.zip',
'C:\Users\chand\AppData\Local\Programs\Python\Python311\DLLs',
'C:\Users\chand\AppData\Local\Programs\Python\Python311\Lib',
'C:\Users\chand\AppData\Local\Programs\Python\Python311',
'C:\Users\chand\venv',
'C:\Users\chand\venv\Lib\site-packages']
Server time: Mon, 10 Jul 2023 10:24:46 +0000

This was the html template I wrote:

{% extends "blog/base.html" %}
{% block content %}
    {% for post in posts %}
        <article class="media content-section">
          <img class="rounded-circle article-img" src="{{ post.author.profile.image.url }}">
          <div class="media-body">
            <div class="article-metadata">
              <a class="mr-2" href="#">{{ post.author }}</a>
              <small class="text-muted">{{ post.date_posted|date:"F d, Y" }}</small>
            </div>
            <h2><a class="article-title" href="{% url 'post_detail' post.id %}">{{ post.title }}</a></h2>
            <p class="article-content">{{ post.content }}</p>
          </div>
        </article>
    {% endfor %}

    {% if is_paginated %}
        {% if page_obj.has_previous %}
          <a class="btn btn-outline-info mb-4" href="?page=1">First</a>
          <a class="btn btn-outline-info mb-4" href="{{ page_obj.previous_page_number }}">Previous</a>
        {% endif %}

        {% for num in page_obj.paginator.page_range %}
          {% if page_obj.number == num %}
            <a class="btn btn-info mb-4" href="?page={{ num }}">{{num}}</a>   
          {% elif num > page_obj.number | add:'-3' and num < page.obj.number | add:'3' %}  
            <a class="btn btn-outline-info mb-4" href="?page={{ num }}">{{num}}</a>   
          {% endif %}
        {% endfor %}

          {% if page_obj.has_next %}
            <a class="btn btn-outline-info mb-4" href="{{ page_obj.next_page_number}}">Next</a>
            <a class="btn btn-outline-info mb-4" href="{{ page_obj.paginator.num_pages }}">Last</a>
          {% endif %}
    {% endif %}
{% endblock content %}

Could someone help me out on this template error?

答案1

得分: 0

你可以在if标签中使用'or',但不能使用' | ',如果这是你的目标:

{% elif num > page_obj.number | add:'-3' and num < page.obj.number | add:'3' %}

但是:在任何情况下,'add:'xyz'' 表达式都不适用于条件。

英文:

you can use 'or' but not '|' in if tags if that is your target:

{% elif num > page_obj.number | add:'-3' and num < page.obj.number | add:'3' %}

but: in any case the add:'xyz' expression does not fit to a condition.

huangapple
  • 本文由 发表于 2023年7月10日 18:51:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76652993.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定