【问题标题】:how to fix "Flask 404" for Routing GET POST如何修复路由 GET POST 的“Flask 404”
【发布时间】:2019-09-25 11:25:53
【问题描述】:

我从 Flask 开始。 我有以下 view.py

我的问题是关于路线 @app.route('/uploaded'...

from flask import render_template, jsonify, Flask, redirect, url_for, request
from app import app
import random
import os
from keras.applications.resnet50 import ResNet50
from keras.preprocessing import image
from keras.applications.resnet50 import preprocess_input, decode_predictions
import numpy as np


@app.route('/')



@app.route('/upload')
def upload_file2():
   return render_template('index.html')

@app.route('/uploaded', methods = ['GET', 'POST'])
def upload_file():
   if request.method == 'POST':
      f = request.files['file']
      path = os.path.join(app.config['UPLOAD_FOLDER'], f.filename)
      model= ResNet50(weights='imagenet')
      img = image.load_img(path, target_size=(224,224))
      x = image.img_to_array(img)
      x = np.expand_dims(x, axis=0)
      x = preprocess_input(x)
      preds = model.predict(x)
      preds_decoded = decode_predictions(preds, top=3)[0] 
      print(decode_predictions(preds, top=3)[0])
      f.save(path)
      return render_template('uploaded.html', title='Success', predictions=preds_decoded, user_image=f.filename)


@app.route('/index')
def index():
    return render_template('index.html', title='Home')

@app.route('/map')
def map():
    return render_template('map.html', title='Map')


@app.route('/map/refresh', methods=['POST'])
def map_refresh():
    points = [(random.uniform(48.8434100, 48.8634100),
               random.uniform(2.3388000, 2.3588000))
              for _ in range(random.randint(2, 9))]
    return jsonify({'points': points})


@app.route('/contact')
def contact():
    return render_template('contact.html', title='Contact')

在.html文件中,经过一些验证步骤后,我要求End Under上传一个文件(一张图片)。

然后在我下面的代码中,输入是图像,我希望输出结果,它必须显示在上传中。

但是,当我访问 localhost:5000 时,我无法访问 uploaded.html

URL 是否卡在我的 PHP 请求中(用于在我的 home.html 中上传文件的请求)

我从这个网站上阅读了许多关于类似问题的不同答案,但没有一个能解决我的问题。

提前致谢

index.html:

{% extends "layout.html" %}

{% block head %}
    {{ super() }}
{% endblock %}

{% block content %}
<h1 class= "ui header" >{{ title }} </h1>
{% if current_user.is_authenticated %}


<!DOCTYPE html>
<html>
<body>

<form action="upload.php" method="post" enctype="multipart/form-data">
    Select image to upload:
    <input type="file" name="fileToUpload" id="fileToUpload">
    <input type="submit" value="Upload Image" name="submit">
</form>

</body>
</html>

<h2 class= "ui header" > Hi {{ title }} </h2>


    {% endif %}

{% endblock %}

日志:

 * Debugger is active!  * Debugger PIN: 127-013-588
127.0.0.1 - - [25/Sep/2019 22:12:18] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [25/Sep/2019 22:12:18] "GET /_debug_toolbar/static/css/toolbar.css?0.2942939835023174 HTTP/1.1" 200 -
127.0.0.1 - - [25/Sep/2019 22:12:26] "POST /upload.php HTTP/1.1" 404 -

【问题讨论】:

  • 你在顶部有一个孤独的路由装饰器(@app.route('/')),正常吗?

标签: python html post flask get


【解决方案1】:

在您的 view.py 中,在索引级别,试试这个:

@app.route('/index')
@app.route('/')
def index():
    return render_template('index.html', title='Home')

您可以通过输入 localhost:5000localhost:5000/index

访问该站点

如@frankie567 所指出的,删除顶部的路线 (@app.route('/'))。

【讨论】:

  • 嗨@Tobin 感谢您的回复,但仍然......同样的错误404。
  • 您能在控制台中显示完整的错误吗?否则很难确定这个错误来自哪里。但通常会使用 404 错误来表示找不到资源。所以要么你指向错误的 url,要么你的应用程序没有运行,...
  • 嗨,@Tobin 再次在上面找到它,我写的。感谢您的宝贵时间
  • 这里有一些快速的建议: 1. 尝试在另一个端口号上运行flask,例如5500。 2.清除浏览器缓存并测试...告诉我它是否有效
【解决方案2】:

使用不同的端口号对我有用!不知道为什么会这样,因为我有一个在 8000 端口上运行的替代烧瓶应用程序。谢谢。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-08
    • 2019-10-08
    • 2022-10-24
    • 2012-02-02
    • 2021-12-29
    • 2014-11-03
    • 2022-01-14
    • 1970-01-01
    相关资源
    最近更新 更多