鍍金池/ 問答/Python  網(wǎng)絡(luò)安全  HTML/ 在flask jinja2模板中報錯顯示 'url_args' is undef

在flask jinja2模板中報錯顯示 'url_args' is undefined

這個是我flask代碼:

coding=utf-8

from flask import render_template, request,flash, redirect, url_for,current_app,abort
from . import main
from ..import db
from ..models import Post, Comment
from flask_login import login_required, current_user
from .forms import CommentForm, PostForm
from flask_babel import gettext as _

@main.errorhandler(404)
def page_not_found(error):

return render_template('404.html'), 404

@main.route('/')
def index():

page_index = request.args.get('page', 1, type=int)

query = Post.query.order_by(Post.created.desc())

pagination = query.paginate(page_index, per_page=20,error_out=False)

posts = pagination.items

return render_template('index.html',
                       title=_(u'歡迎來到Lee的投研咖啡館'),
                       posts=posts,
                       pagination=pagination)

這個是我的模板代碼:
{% extends 'bootstrap/base.html' %}
{% block title %}{{ title }}{% endblock %}
{% block head %}

{{ super() }}
{% include ['includes/_head.html', 'includes/_metas.html'] %}

{% endblock %}
{% block styles %}

{{ super() }}
<link rel="stylesheet"
      >

{% endblock %}
{% block navbar %}

{# { nav.top.render() }} #}
{% include 'includes/_navbar.html' %}

{% endblock %}

{% block content %}

{% block header %}
    <header>
        <div class="page-header">
            <div class="container">
                <h1>{{ title }}</h1>
            </div>
        </div>
    </header>
{% endblock %}
{% block page_body %}
{% endblock %}

{% endblock %}

這個是我的錯誤顯示:

clipboard.png

clipboard.png

clipboard.png

是我使用的方式不對么?請各位解答

回答
編輯回答
臭榴蓮

問題已經(jīng)解決了,錯誤在index.html中pagination用法的錯誤,原來的代碼是:
{% if pagination %}

        {{ render_pagination(pagination) }}
    {% endif %}

已經(jīng)改正:
{% if pagination %}
<div class="pagination">

{{ macros.pagination_widget(pagination,'.index') }}

</div>
{% endif %}

同時,修改了macros宏分頁模板:{#% marco input(name,value='',type='text',size=20) %#}
{% macro pagination_widget(pagination, endpoint, fragment='') %}

<!--input type="{{ type }}"
       name="{{ name }}"
       value="{{ value }}"
       size="{{ size }}"
       /-->

<ul class="pagination">

<li{% if not pagination.has_prev %} class="disabled"{% endif %}>
    <a href="{% if pagination.has_prev %}{{ url_for(endpoint, page=pagination.prev_num, **kwargs) }}{{ fragment }}{% else %}#{% endif %}">
        &laquo;
    </a>
</li>
{% for p in pagination.iter_pages() %}
    {% if p %}
        {% if p == pagination.page %}
        <li class="active">
            <a href="{{ url_for(endpoint, page = p, **kwargs) }}{{ fragment }}">{{ p }}</a>
        </li>
        {% else %}
        <li>
            <a href="{{ url_for(endpoint, page = p, **kwargs) }}{{ fragment }}">{{ p }}</a>
        </li>
        {% endif %}
    {% else %}
    <li class="disabled"><a href="#">&hellip;</a></li>
    {% endif %}
{% endfor %}
<li{% if not pagination.has_next %} class="disabled"{% endif %}>
    <a href="{% if pagination.has_next %}{{ url_for(endpoint, page=pagination.next_num, **kwargs) }}{{ fragment }}{% else %}#{% endif %}">
        &raquo;
    </a>
</li>

</ul>

{% endmacro %}
注釋掉的是原來的內(nèi)容

2017年11月7日 09:07