如何使用 Flask 通过 HTML 表单携带带空格的字符串

2023-10-20前端开发问题
16

本文介绍了如何使用 Flask 通过 HTML 表单携带带空格的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在尝试使用 Flask 和 Python 3.6 构建一个简单的在线测验,使用带有单选按钮的 HTML 表单在 Flask 路由之间传递选定的答案.第一步是为测验选择一个类别,然后进入实际的测验页面,如下所示:

I'm trying to build a simple online quiz using Flask and Python 3.6, using HTML forms with radio buttons to carry the selected answers between Flask routes. The first step is to select a category for the quiz, before leading to the actual quiz page, as follows:

app = Flask(__name__)
categories = ['Europe', 'South America', 'North America']
@app.route('/')
def select():
    return render_template('selecting.html', cats= categories)

@app.route('/quiz', methods = ['POST'])
def quizing():
    selected_cat = request.form['categories']
    return "<h1>You have selected category: " + selected_cat + "</h1>

其中'selecting.html'如下:

Where 'selecting.html' is as follows:

<form action='/quiz' method='POST'>
<ol>
{% for cat in cats%}
    <li><input type = 'radio' name= 'categories' value ={{cat}}>{{cat}}</li>
{% endfor %}
</ol>
<input type="submit" value="submit"/>
</form>

当我选择欧洲"时,测验页面显示:

When I select 'Europe', the quiz page reads:

<h1>You have selected category: Europe</h1>

但是,当我选择北美"时,测验页面显示:

However, when I select 'North America' the quiz page reads:

<h1>You have selected category: North</h1>

为什么Flask路由之间没有携带所选类别的第二个单词,如何保留完整的类别名称?

Why is the second word of the selected category not carried between the Flask routes and what can I do to retain the full category name?

推荐答案

根据HTML5 文档,未加引号的属性不能有嵌入空格.

According to the HTML5 documentation, an unquoted attribute must not have an embedded space.

您的 input 元素扩展为以下文本:

Your input element expands to the following text:

<input type = 'radio' name= 'categories' value =North America>

即使你的意思是它有一个 value 属性,其值为 North America,它实际上有一个 value 属性,其值为North 的值和具有空值的 America 属性.

Even though you mean for it to have a value attribute with a value of North America, it actually has a value attribute with a value of North and a America attribute with an empty value.

尝试引用 value 属性值:

<li><input type = 'radio' name= 'categories' value ="{{cat}}">{{cat}}</li>

这篇关于如何使用 Flask 通过 HTML 表单携带带空格的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

layui 实现实时刷新一个外部的div
主页面上显示了一个合计,在删除和增加的时候需要更改这个总套数的值: //html代码div class="layui-inline layui-show-xs-block" style="margin-left: 10px" id="sumDiv"spanSOP合计:/spanspan${totalNum}/spanspan套/span/div 于是在我们删除这个条数据后,...
2024-11-14 前端开发问题
156

layui要如何改变时间日历布局大小?
问题描述 我想改变layui时间日历布局大小,这个要怎么操作呢? 解决办法 可以用css样式对时间日历进行重新布局,具体代码如下: !DOCTYPE htmlhtmlheadmeta charset="UTF-8"title/titlelink rel="stylesheet" href="../../layui/css/layui.css" /style#test-...
2024-10-24 前端开发问题
271

jQuery怎么动态向页面添加代码?
append() 方法在被选元素的结尾(仍然在内部)插入指定内容。 语法: $(selector).append( content ) var creatPrintList = function(data){ var innerHtml = ""; for(var i =0;i data.length;i++){ innerHtml +="li class='contentLi'"; innerHtml +="a href...
2024-10-18 前端开发问题
125

“数组中的每个孩子都应该有一个唯一的 key prop"仅在第一次呈现页面时
quot;Each child in an array should have a unique key propquot; only on first time render of page(“数组中的每个孩子都应该有一个唯一的 key prop仅在第一次呈现页面时)...
2024-04-20 前端开发问题
5

如何制作 TextGeometry 多线?如何将它放在一个正方形内,以便它像 html 文本一样包裹在 div 内?
How do I make a TextGeometry multiline? How do I put it inside a square so it wraps like html text does inside a div?(如何制作 TextGeometry 多线?如何将它放在一个正方形内,以便它像 html 文本一样包裹在 div 内?) - IT屋-程序员软件开发技术分享社...
2024-04-20 前端开发问题
6

缩放背景图像以适合 ie8 窗口
Scale background image to fit ie8 window(缩放背景图像以适合 ie8 窗口)...
2024-04-19 前端开发问题
11