Python Flask WTForm SelectField产生404错误

Python Flask WTForm SelectField产生404错误,python,flask,http-status-code-404,wtforms,Python,Flask,Http Status Code 404,Wtforms,我花了几个小时在这不知道为什么我得到一个404错误。我会尽量说清楚的。 这是我的文件夹结构 project--> run.py app--> forms.py views.py templates--> Crew.html static--> Crews-->

我花了几个小时在这不知道为什么我得到一个404错误。我会尽量说清楚的。 这是我的文件夹结构

project-->
       run.py
       app-->
            forms.py
            views.py
            templates-->
                       Crew.html
            static-->
                    Crews-->
                            1901.json
                            1902.json
                            1903.json
在forms.py中,这是创建和填充表单的方式

    import os
from flask import current_app
from flask_wtf import Form
from wtforms import SelectField


class CrewForm(Form):
    filename = SelectField()

    def __init__(self, *args, **kwargs):
        root = os.path.join(current_app.static_folder, 'Crews')
        choices = [(f, f) for f in os.listdir(root) if os.path.isfile(os.path.join(root, f))]
        self.filename.kwargs['choices'] = choices
        super(CrewForm, self).__init__(*args, **kwargs)
在views.py中,这是@app.route

@app.route('/CastCrew', methods=['GET', 'POST'])
def crew():
    form = CrewForm()

    if form.validate_on_submit():
        return current_app.send_static_file(os.path.join('Crews', form.filename.data))

    return render_template('Crew.html', form=form)
在Crew.html中,表单就是这样创建的

<form method="post">
  {{ form.hidden_tag() }} <!--CSFR config -->
      Please choose a year:<br>
      {{ form.filename }}<br>
当我选择一个年份并按Submit时,在cmd提示符中我得到了这个错误-
POST/CastCrew HTTP/1.1 404-

在网页上我发现了这个错误

Not Found The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.
如果表单填充正确,为什么会出现404错误?任何帮助都将不胜感激

我在web控制台中收到此错误消息

The character encoding of the HTML document was not declared. The `document will render with garbled text in some browser configurations if the document contains characters from outside the US-ASCII range. The character encoding of the page must be declared in the document or in the transfer protocol.`
run.py

from app import app
app.run(debug=True)
在run.py的末尾添加了
print(app.url\u map)
,得到了这个

    * Restarting with reloader
Map([<Rule '/CastCrew' (HEAD, POST, OPTIONS, GET) -> crew>,
 <Rule '/index' (HEAD, OPTIONS, GET) -> index>,
 <Rule '/about' (HEAD, OPTIONS, GET) -> about>,
 <Rule '/data' (HEAD, POST, OPTIONS, GET) -> data>,
 <Rule '/' (HEAD, OPTIONS, GET) -> index>,
 <Rule '/static/<filename>' (HEAD, OPTIONS, GET) -> static>])
*使用重新加载程序重新启动
地图([机组>,,
索引>,
大约>,,
数据>,,
索引>,
静态>])

这个问题被标记为一个重复的问题,并链接到我之前提出的一个问题-它是不同的,因为它是由另一个用户编辑的,因为另一个问题出现,使它不同

在我看来,你的问题至少有一部分可能是缩进。你有:

@app.route('/CastCrew', methods=['GET', 'POST'])
def crew():
    form = CrewForm()

if form.validate_on_submit():
    return current_app.send_static_file(os.path.join('Crews', form.filename.data))

return render_template('Crew.html', form=form)
何时应该:

@app.route('/CastCrew', methods=['GET', 'POST'])
def crew():
    form = CrewForm()

    if form.validate_on_submit():
        return current_app.send_static_file(os.path.join('Crews', form.filename.data))

    return render_template('Crew.html', form=form)

使用
crew()函数中的表单提交代码。

是否需要使用javascripts

我对你的代码做了一点修改,对我来说效果很好

Crew.html

<form method="post">
   {{ form.hidden_tag() }} <!--CSFR config -->
   Please choose a year:<br>
   {{ form.filename }}<br>
   <input type="submit">
</form>
如果这没有帮助,您需要更好地跟踪问题。“未找到”错误表示文件未位于您认为的位置。添加一些日志或打印,帮助您找到导致崩溃的行。

如果您在Windows上,我认为您的主要问题是缺少
/
字符

在这一行

return current_app.send_static_file(os.path.join('Crews', form.filename.data))
在Windows上,
os.path.join('crows',form.filename.data)
将使用Windows分隔符创建一个路径,即
crows\.json
,该分隔符将以主机名作为前缀。但是,您需要一个URL样式的子路径,即
crows/.json

您应该使用文件名加入
Crews/
,而不是
Crews
。404告诉您它找不到要返回的文件(因为路径无效)。可能还有其他错误,因为我们不知道您的
run.py
等的完整设置

试试这句话

return current_app.send_static_file(os.path.join('Crews/', form.filename.data))

您不应该使用
POST
作为表单的方法,因为您没有持久化任何内容
GET
请求可以附带数据来指定您想要从服务器返回的内容。在注册路由后的
run.py
中,
print(app.url\u map)
的输出是什么?@Sean Vieira我已将其添加到问题中:)抱歉,Rory,我粘贴代码时一定是缩进错了,文件中缩进正确:)啊,对不起!我的错误!:)除了def Crew()的缩进外,该代码与我的代码相同:?文件就在那里表单中填充了Crews文件夹中的文件名,这可能与编码有关吗?添加了“\”但没有任何区别,我现在在问题中添加了run.py代码:)注意斜杠的方向。对不起,输入错误,意思是写“/”好的,我退出了。这里有许多潜在的问题,如果没有一个最小的、完整的和可验证的示例,就很难提供帮助。我有一个版本可以在我的机器上实现我认为您想要的功能,但它需要
/
更正,还将文件夹
app
的名称更改为
myapp
,以避免与
app
Flask
对象混淆,对导入进行假设,在表单中添加了一个操作
CrewCast
(为了不让d3变得复杂)。你需要做一个简单的测试,并描述你尝试过的调试,哪一行失败了等等…顺便说一句-你现在有一个关于另一个问题的工作示例(指定为重复),它对上面评论中提到的(可能)进行了更多的修改。
return current_app.send_static_file(os.path.join('Crews', form.filename.data))
return current_app.send_static_file(os.path.join('Crews/', form.filename.data))