【问题标题】:404 Error in flask webapp unable to find reason烧瓶 webapp 中的 404 错误无法找到原因
【发布时间】:2019-07-15 23:59:16
【问题描述】:

运行 webapp 时出现 404 错误,即使将其更改为最基本的模板,该错误甚至不允许我的错误 html 运行。我不知道在哪里找不到我的请求。

我尝试从 base.html 中删除所有样式表以查看是否是这些样式表,我尝试更改 error.html 以查看它是否会真正运行我尝试注释掉 html 文件中的所有 url_for。

我把结果注释掉了,所以我不这么认为。

routes.py


    from app import app
    from flask import Flask, abort, jsonify, redirect, url_for, request, render_template
    from werkzeug.exceptions import HTTPException
    from app.results import clean_data, get_response


    @app.errorhandler(Exception)
    def handle_error(e):
        '''
        code = 500
        if isinstance(e, HTTPException):
            code = e.code'''
        print(str(e))
        return render_template("error.html")


    @app.route('/', methods=['GET', 'POST'])
    def index():
        data = clean_data(request.form)
        response = get_response(data)
        print(response)
        return render_template("index.html")

base.html


    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="utf-8">


            <title>Default Scorer -- Ebury</title>
            <meta name = "viewport" content = "width=device-width", initial-scale = 1, shrink-to-fit=no">
            <link rel="icon" href="{{ url_for('static', filename='favicon.ico') }}">
            <!-- CSS-->
            <link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}">
            <link href="https://fonts.googleapis.com/css?family=Roboto:100" rel="stylesheet">
            <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous">
            <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
            <!-- JS-->
            <!-- [if lt IE 9]><script src="https://cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv.min.js" type="text/javascript"></script><![endif] -->
            <script src="https://ebury-chameleon.s3.amazonaws.com/1.17.0/scripts/ebury-chameleon.js" type="text/javascript"></script>

        </head>

        <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
        <a class="navbar-brand" href="/">
            <img src="{{ url_for('static', filename='img/Ebury.png') }}"  width="80" height="30" alt="">
        </a>
        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavAltMarkup" aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
        </button>
        <div class="collapse navbar-collapse" id="navbarNavAltMarkup">
            <div class="navbar-nav">
                <a class="nav-item nav-link active" href="/"><b>Default Scorer</b> <span class="sr-only">(current)</span></a>
            </div>
            <div class="navbar-nav ml-auto">
                <a class="nav-item nav-link" href="/logout">Log out</a>
            </div>
        </div>
        </nav>

        <body>
        </body>

</html>

index.html(只有相关部分有很多表格)

{% extends "base.html" %}

{% block title %}Index{% endblock %}

{% block content %}

            <div class="m-1 pb-3"></div>

            <div class="container-fluid">
                <div class = "row">
                    <div class="col-md-3 push-md-3">
                        <div class = "m-0 pb-0">
                            <form action="" method="post" role="form" class="needs-validation" novalidate>
                                <div class="container">
                                    <div class = "form-group">
                                        <div class = "row">
                                            <div class="input-group input-group-sm mb-3">
                                                <div class="input-group-prepend">
                                                    <span class = "input-group-text">BVD ID Number</span>
                                                </div>
                                                <input type = "text" class = "form-control" id = "bvd_id_number" name = "bvd_id_number">
                                            </div>
                                        </div>
                                    </div>
                                 </div>
                              </form>
                            </div>
                         </div>
                     </div>
                  </div>
    {% endblock %}

    {% block scripts %}

    <script>
        (function() {
            'use strict';
            window.addEventListener('load', function() {
                // Fetch all the forms we want to apply custom Bootstrap validation styles to
                var forms = document.getElementsByClassName('needs-validation');
                // Loop over them and prevent submission
                var validation = Array.prototype.filter.call(forms, function(form) {
                    form.addEventListener('submit', function(event) {
                        if (form.checkValidity() === false) {
                            event.preventDefault();
                            event.stopPropagation();
                        }
                        form.classList.add('was-validated');
                    }, false);
                });
            }, false);
        })();
    </script>

    {% endblock %}

error.html


    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>ERROR</title>
    </head>
    <body>
        <h1>ERROR</h1>
    </body>
    </html>

所以发生的情况是表单没有出现,似乎只有 base.html 没有其他内容出现,图标也没有出现,但我尝试将其注释掉,但不起作用。

打印的错误是“404 Not Found:在服务器上找不到请求的 URL。如果您手动输入了 URL,请检查您的拼写并重试”

【问题讨论】:

  • 这可能是一个不同的问题,但是 index.html 是否扩展了 base.html?无论如何,我在 base.html 中没有看到 {%block content%}。
  • 是的,很抱歉,我没有在代码中包含它,我认为我只是从 bt 低点复制粘贴
  • 这看起来像是一个路由问题。由于您的错误处理程序也没有被调用,因此请尝试验证您实际上正在使用正确的文件。好像没有加载这个路由文件。
  • 什么意思?当我更改发生更改的某些内容时,我正在运行正确的文件。它以前运行过,我认为我没有改变任何东西
  • 我的意思是我过去设法解决了错误的文件。

标签: python html flask


【解决方案1】:

致未来的人:我解决了,因为 base.html 没有指定它必须在哪里扩展。正确的 base.html 代码是:

    <html>
        <head>
            <meta charset="utf-8">


            <title>Default Scorer -- Ebury</title>
            <meta name = "viewport" content = "width=device-width", initial-scale = 1, shrink-to-fit=no">
            <link rel="icon" href="{{ url_for('static', filename='favicon.ico') }}">
            <!-- CSS-->
            <link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}">
            <link href="https://fonts.googleapis.com/css?family=Roboto:100" rel="stylesheet">
            <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous">
            <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
            <!-- JS-->
            <!-- [if lt IE 9]><script src="https://cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv.min.js" type="text/javascript"></script><![endif] -->
            <script src="https://ebury-chameleon.s3.amazonaws.com/1.17.0/scripts/ebury-chameleon.js" type="text/javascript"></script>

        </head>

        <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
        <a class="navbar-brand" href="/">
            <img src="{{ url_for('static', filename='img/Ebury.png') }}"  width="80" height="30" alt="">
        </a>
        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavAltMarkup" aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
        </button>
        <div class="collapse navbar-collapse" id="navbarNavAltMarkup">
            <div class="navbar-nav">
                <a class="nav-item nav-link active" href="/"><b>Default Scorer</b> <span class="sr-only">(current)</span></a>
            </div>
            <div class="navbar-nav ml-auto">
                <a class="nav-item nav-link" href="/logout">Log out</a>
            </div>
        </div>
        </nav>

        <body>
        {% block content %}
        {% endblock %}
        </body>
{% block scripts %}
{% endblock %}
</html>```

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-18
    • 2015-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多