【问题标题】:How to let Python and HTML recognize both Uppercase and lowercase letters?如何让Python和HTML同时识别大写和小写字母?
【发布时间】:2020-04-20 09:44:42
【问题描述】:

我在 python 烧瓶和 html 文件中创建了一个小应用程序,当我想搜索特定字段时,它只在大小写匹配后搜索,即如果我想搜索印度,它只会在大小写匹配,但是当我搜索印度或印度时,它没有被搜索到?没有显示结果的原因可能是什么? 后来我从stackoverflow的答案中发现,我需要先将其转换为大写,这是答案How to let Python recognize both lower and uppercase input?

但是如何在这里包含它?

<input type="text" class="form-control" id="search" name="search" placeholder="search"
                                       value="{{ request.form['search'] }}"></td>

在我的 py 文件中,它的 if request.method == 'POST': search = request.form['search'].casefold() request.form['search'] 中的内容如何转换为 .casefold() 或 .upper() 或 .lower()。请帮帮我。

【问题讨论】:

  • 希望你能找到答案here

标签: python html python-3.x flask


【解决方案1】:

尝试使用:

<input type="text" class="form-control" id="search" name="search" placeholder="search"
                                       value="{{ request.form['search'].upper() }}"></td>

<input type="text" class="form-control" id="search" name="search" placeholder="search"
                                       value="{{ request.form['search'].lower() }}"></td>

基本上,您要做的就是将“输入”和“您尝试匹配的任何内容”转换为相同的大小写格式,即。大写或小写或标题。 我想您还必须在进行实际搜索的后端逻辑中进行相同的更改。

【讨论】:

  • 它不起作用。也改变了我的后端。就像if request.method=='POST': search = request.form['search'] 所以当我通过打印搜索进行交叉验证时。它不会改变
  • 是的....如果 request.method=='POST': search = request.form['search'].upper() 并且如果您使用的是 sql,这就是您需要实际更改的地方在数据库中搜索一些东西然后使用这个:“select * from users where upper(first_name) = '"+search+"';"虽然从安全的角度来看,上面的代码也是不好的做法,你应该使用字符串格式,但我不记得它是如何工作的,所以我是这样写的。
【解决方案2】:

你可以使用 str.upper() 方法:

{% elif  student.department.upper() != "MATHS DEPARTMENT" %}
    Maths department
{% endif %}

同样适用于小写

similarly, student.department.lower() != "maths department" 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-25
    • 2016-04-28
    • 2015-10-24
    • 2013-02-04
    • 1970-01-01
    • 1970-01-01
    • 2019-02-15
    • 1970-01-01
    相关资源
    最近更新 更多