【发布时间】: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上的项目链接
【问题讨论】: