【问题标题】:Jinja2/Flask url_for with 4 parameters creates a GET request带有 4 个参数的 Jinja2/Flask url_for 创建一个 GET 请求
【发布时间】:2017-06-17 17:47:21
【问题描述】:

我的模板中有以下代码:

{% block body %}
  {% for x in range(0, 4) %}
    <a href="{{url_for('quiz', category=category, id=id2,  type=clean_types[x]|string) }}">
    {{clean_answers[x]}}
    </a>
  {% endfor %}
{% endblock %}

但是,对于我的最后一个参数,而不是获取:

127.0.0.1:5000/quiz/Books/1/True

我明白了:

127.0.0.1:5000/quiz/Books/1?type=True

您能解释一下为什么会发生这种情况,以及我如何解决它吗?

我尝试了使用和不使用|string 转换,我尝试先将一个单独的变量设置为clean_types[x],然后对其进行转换,但它仍然显示为?type=True。

作为参考,clean_types 是一个包含 4 个项目的列表,以各种顺序为 True 或 False,它通过在瓶中返回模板。

生成链接的路径是:

@app.route('/quiz/<category>/<int:id>')

def quiz(category, id):

    questions = list(db_quiz.getQuestionsByCategory(category))

    clean_question = questions[id][1]
    print(id)
    print(id+1)
    print(clean_question)

    dirty_answers = []

    for x in range(0, 4):
        dirty_answers.append(questions[0][2 + x])

    shuffled_answers = random.sample(dirty_answers, len(dirty_answers))

    clean_answers = [i.split(',')[0] for i in shuffled_answers]
    clean_types = [i.split(',')[1] for i in shuffled_answers]

    print("---------------")

    print(clean_answers)
    print(clean_types)

    clean_types_v2 = ('ja', 'nee', 'nee', 'nee')

    id2=id+1
    return flask.render_template('quiz.html',
                                 category=category,
                                 id2=id2,
                                 clean_question=clean_question,
                                 clean_types=clean_types,
                                 clean_answers=clean_answers)

基本模板

<head>
<meta charset="UTF-8">
<title>tinyQuiz</title>
<link rel="icon" type="image/png" href="{{ url_for('static', filename='img/favicon.png') }}" />
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='css/reset.css') }}" />
<link rel=stylesheet type=text/css href="{{ url_for('static', filename='css/style.css') }}" />
{% block head %}{% endblock %}
</head>

<body>
<div class="verticalAlign">
    <img src="{{ url_for('static', filename='img/logo.png') }}" />

    <h1>tinyQuiz{% block h1 %}{% endblock %}</h1><br/>
    <h2>{% block h2 %}{% endblock %}</h2><br id="br" />
    <h3>{% block h3 %}{% endblock %}</h3><br/><br id="br"/>

    {% block body %} {% endblock %}
</div>
<script src="{{ url_for('static', filename='js/jquery-3.2.1.js') }}"></script>
</body>
</html>

扩展它的测验模板:

{% extends "base.html" %}

{% block head %}
<link rel=stylesheet type=text/css href="{{ url_for('static', filename='css/quiz.css') }}" />
{% endblock %}

{% block h2 %} You are playing: {{ category.capitalize() }} {% endblock %}

{% block h3 %} {{ clean_question }} {% endblock %}

{% block body %}

{% for x in range(0, 4) %}
    <a  href="{{url_for('quiz', category=category, id=id2, type=clean_types[x]) }}">
        {{clean_answers[x]}}
    </a>
{% endfor %}
{% endblock %}

链接应该指向的路线是:

@app.route('/quiz/<category>/<int:id>/<type>')
def trivia(category, id, type):
    if type == 'True':
        # scores.append(1) #add one to show you got a question correctly
        return flask.render_template('trivia_true.html')
    else:
        # scores.append(0) #add zero to show you failed to answer correctly
        return flask.render_template('trivia_false.html')

这是github上的项目链接

【问题讨论】:

    标签: python html flask jinja2


    【解决方案1】:

    我看到你的 HTML a href 链接是正确的。但是在你的quiz函数路由url构造错误,应该是……

    @app.route('/quiz/<category>/<int:id>/<type>')
    def quiz(category, id, type):
    

    【讨论】:

    • 我知道它在重定向的路由中,但问题是前一个路由创建的链接不正确。
    • @app.route('/') 创建重定向到 @app.route('/quiz//') 的链接,然后该页面应生成将重定向到 @app.route('/quiz///') 的链接。创建 的路由没有也不需要它作为参数,当我添加它时它甚至不会编译。
    • 请在此处包含相关的最小代码...我开始看
    • 我添加了 base.html 模板和扩展它的 quiz.html 模板。谢谢。
    • 你能粘贴你的 index.html 吗?
    【解决方案2】:

    哦。我的。上帝。

    我很愚蠢,完全误解了 url_for 文档。

    url_for 开头的“东西”指的是烧瓶中的函数/路径,不像我想的那样,它只是 url 的字符串。

    将我的链接更改为

            <a  href="{{ url_for('trivia', category=category, id=id2, type=clean_types[x]) }}">
    

    解决了我的问题。

    现在当然也很明显为什么它会生成 GET 请求而不是普通链接 - 它指的是“自身”,因此同一路径上的新参数将是 GET 请求。

    【讨论】:

      猜你喜欢
      • 2014-01-15
      • 2019-04-10
      • 2012-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-07
      • 2018-03-06
      • 2015-11-21
      相关资源
      最近更新 更多