Flask 404 Not Found错误,但python文件中存在子链接

问题描述 投票:0回答:1

大家好,感谢您花时间阅读并可能帮助我。

我正在运行烧瓶,我的索引页面工作正常,但现在我正在尝试为我的网站设置一些额外的页面,我得到404错误,即使我相当确定我在python应用程序文件中正确设置它们。

以下是错误日志:

2017-12-23 16:32:43,232: werkzeug.routing.BuildError: Could not build url for endpoint 'laws'. Did you mean 'search' instead?
2017-12-23 16:32:43,232: File "/home/*****/.virtualenvs/zones4drones/lib/python3.6/site-‌​packages/flask/app.p‌​y", line 1997, in call

我想知道它是否与DNS有关?或者也许是服务器主机?因为我在测试环境(CS50 IDE)中运行它并且链接确实有效。

这是烧瓶应用程序:

import sqlite3
import os
from datetime import datetime
from flask import Flask, flash, redirect, render_template, request, session, url_for, jsonify
from flask_session import Session
from passlib.apps import custom_app_context as pwd_context
from tempfile impor t gettempdir
from flask_jsglue import JSGlue

import time

# configure application
app = Flask(__name__)
#JSGlue(app)

# configure session to use filesystem (instead of signed cookies)
app.config["SESSION_FILE_DIR"] = gettempdir()
app.config["SESSION_PERMANENT"] = False
app.config["SESSION_TYPE"] = "filesystem"
app.config["DEBUG"] = True

# ensure responses aren't cached
if app.config["DEBUG"]:
    @app.after_request
    def after_request(response):
        response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
        response.headers["Expires"] = 0
        response.headers["Pragma"] = "no-cache"
        response.headers['Cache-Control'] = 'public, max-age=0'
        return response

Session(app)

itemCount = 0
vehicleCount = 0

@app.route("/")
def index():
    return render_template("index.html")

@app.route('/laws', methods=['GET'])
def laws():
    return render_template("laws.html")

@app.route('/about', methods=['GET'])
def about():
    return render_template("about.html")

@app.route("/search")
def search():
    """Search for items that match query."""

    conn = sqlite3.connect('/home/extinkt/zones4drones/airports2.db')
    db = conn.cursor()

    a = float(request.args.get("swLat"))
    b = float(request.args.get("swLong"))
    c = float(request.args.get("neLat"))
    d = float(request.args.get("neLong"))

    if (a < 0): a += .1
    else:       a -= .1

    if (b < 0): b -= .1
    else:       b += .1

    if (c < 0): c -= .1
    else:       c += .1

    if (d < 0): d += .1
    else:       d -= .1

    #print(a + ", " + b + ", " + c + ", " + d)

    db.execute("""SELECT latitude, longitude, name, class FROM airports WHERE
                (CASE WHEN {0} < {2}
                    THEN latitude BETWEEN {0} AND {2}
                    ELSE latitude BETWEEN {2} AND {0}
                END)
                AND
                (CASE WHEN {1} < {3}
                    THEN longitude BETWEEN {1} AND {3}
                    ELSE longitude BETWEEN {3} AND {1}
                END)
                AND (type != 'heliport' AND type != 'balloonport' AND type != 'closed')""".format(a, b, c, d))

    airports = db.fetchall()

    db.close()
    conn.close()

    return jsonify(airports)

因为有人要求我在这里包含index.html(以及包装index.html的layout.html):

{% block main %}
    <div id="container">
        <div id="main">
            <div id="left-bar">
                <div id="flightCheckBox">
                    <div id="listTitle">
                        <img id="listTitleImg" src="{{ url_for('static', filename='img/listTitle.png') }}"/>
                    </div>
                    <div id="checklist">
                        <div id="listItem">
                            <div class="check">All wiring connections are intact</div>
                            <div class="check">Battery charged and securely strapped, no loose wires</div>
                            <div class="check">Well clear of all obstructions (Trees, Phone Lines, Radio Interference)</div>
                            <div class="check">No people/animals in flight path (Have a spotter if unsure)</div>
                            <div class="check">Not impeeding any emergency operations (Fires, Accidents, Rescues)</div>
                            <div class="check">Weather conditions suitable for flight (Rain, Wind, Fog) </div>
                            <div class="check">Location check complete (not within 5 miles of an Airport)</div>
                        </div>
                        <div id="checkboxes">
                            <div id="firstCheckbox" class="checkbox"><img src="{{ url_for('static', filename='img/unchecked.png') }}"/></div>
                            <div class="checkbox"><img src="{{ url_for('static', filename='img/unchecked.png') }}"/></div>
                            <div class="checkbox"><img src="{{ url_for('static', filename='img/unchecked.png') }}"/></div>
                            <div class="checkbox"><img src="{{ url_for('static', filename='img/unchecked.png') }}"/></div>
                            <div class="checkbox"><img src="{{ url_for('static', filename='img/unchecked.png') }}"/></div>
                            <div class="checkbox"><img src="{{ url_for('static', filename='img/unchecked.png') }}"/></div>
                            <div id="locationCheck" class="checkbox"><img id="locationCheckImg" src="{{ url_for('static', filename='img/unchecked.png') }}"/></div>
                        </div>    
                    </div>
                    <div id="resultImg">
                        <img id="resultCheckImg" src="{{ url_for('static', filename='img/flightCheckIncomplete2.png') }}"/>
                    </div>
                </div>
                <div id="videoFull">
                    <div id="titleDiv"><img id="videoTitle" src="{{ url_for('static', filename='img/videoTitle.png') }}"/></div>
                    <div id="videoWrapper">
                        <video id="videoFrame" controls="controls" autoplay="true" name="Steetly Session" 
                        src="{{ url_for('static', filename='video/steetley-edit.mov') }}"></video>
                        <!-- <iframe id="videoFrame" src="https://www.youtube.com/embed/TnXZEcU7PBw?autoplay=1&modestbranding=1&controls=0" allowfullscreen></iframe> -->
                    </div>
                </div>
                <div id="infoBox">
                    <p class="info2"style="padding-top: 10px">Want to see your video up there?</p>
                    <p class="info2">Click <a href=''>HERE</a> to submit your video</p>
                    <p class="info2">for a chance to be displayed!</p>
                    <p class="info2" style="padding-top: 18px">Did we miss a zone or screw something up?</p>
                    <p class="info2">It happens.  Let us know, <a href=''>HERE</a>!</p>
                <!--
                    <p class="info2" style="padding-top: 18px">If you like what we're doing here,</p>
                    <p class="info2">consider buying us a coffee or two.</p>
                    <div id="ko-fi" align="center"><script type='text/javascript' src='https://ko-fi.com/widgets/widget_2.js'></script><script type='text/javascript'>kofiwidget2.init('Buy Us a Coffee', '#45829E', 'S6S45ZA5');kofiwidget2.draw();</script></div>
                -->
                </div>
            </div>  
            <div id="right-bar">
                <!-- Map -->
                <div id="map">

                    <!-- https://developers.google.com/maps/documentation/javascript/tutorial -->
                    <div id="map-canvas"></div>

                    <!-- http://getbootstrap.com/css/#forms -->
                    <form id="form">
                        <div class="form-group">
                            <label class="sr-only" for="q" style="color: #26c9dc">Search Locations</label>
                            <input class="form-control" id="q" placeholder="Search Locations" autocomplete="off" type="text"/>

                        <div id="geoLocate">
                            <button id="geoButton"><img src="{{ url_for('static', filename='img/geoLocate.png') }}" height="20px" /></button>
                        </div>
                    </form>
                </div>
                <div id="legend">
                    <table style="width:100%">
                        <tr>
                            <th class="head"><img class="circle" src="{{ url_for('static', filename='img/circle_blue.png') }}"/></th>
                            <td><p class="zoneInfo">Commercial Airports</p></td>
                            <td class="head"><img class="circle" src="{{ url_for('static', filename='img/circle_red.png') }}"/></td>
                            <td><p class="zoneInfo">Military Airports</p></td>
                        </tr>
                        <tr>
                            <th class="head"><img class="circle" src="{{ url_for('static', filename='img/circle_yellow.png') }}"/></th>
                            <td><p class="zoneInfo">National Parks</p></td>
                            <td class="head"><img class="circle" src="{{ url_for('static', filename='img/circle_green.png') }}"/></td>
                            <td><p class="zoneInfo">Civilian Marked Zones</p></td>
                        </tr>
                    </table>
                </div>
            </div>
        </div>

    </div>
{% endblock %}

这是包装每个页面的layout.html:

<!DOCTYPE html>

<html lang="en">

    <head>

        <link rel="shortcut icon" href="{{ url_for('static', filename='img/favicon.ico') }}">

        <!-- http://jquery.com/ -->
        <script src="https://code.jquery.com/jquery-latest.min.js"></script>

        {% block script %}{% endblock %}

        {% block css %}{% endblock %}

        <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

        <title>{% block title %}{% endblock %}</title>

</FRAMESET>
    </head>

    <body>

        <div class="container">
            <div id="header">
                <div id="header-content">
                    <div id="logo" align="left">
                        <img id="logoImg" src="{{ url_for('static', filename='img/logo3.png') }}"/>
                    </div>
                    <div id="menu">
                        <a href="{{ url_for('index') }}"> 
                            <img class="option" src="{{ url_for('static', filename='img/checklist.png') }}"/>
                        </a>
                        <a href="{{ url_for('laws') }}">
                            <img class="option" src="{{ url_for('static', filename='img/laws.png') }}"/>
                        </a>
                        <a href="{{ url_for('about') }}">
                            <img class="option" src="{{ url_for('static', filename='img/about.png') }}"/>
                        </a>
                    </div>
                </div>
                <div id="seperator-bar"></div>
            </div>

            <main>
                {% block main %}{% endblock %}
            </main>
        </div>
    </body>
</html>

这里是laws.html(它还没有任何内容,我只是想加载它),以防有助于:

{% extends "layout.html" %}

{% block css %}
    <link href="{{ url_for('static', filename='index.css') }}" rel="stylesheet"/>
{% endblock %}

{% block script %}  
    <!-- app's own JavaScript -->
    <script src="{{ url_for('static', filename='scripts.js') }}"></script>

{% endblock %}

{% block title %}
    Zones 4 Drones
{% endblock %}

{% block main %}
    <div id="container">
        <div id="main">
            <h1>Hello</h1>
        </div>
    </div>
{% endblock %}

这是服务器模板目录中ls的输出:

14:26 ~/zones4drones/templates $ ls
about.html  index.html  laws.html  layout.html

所有这4个文件都具有权限:

-rw-rw-r--

如果看到该网站可能有所帮助,它位于zonesfordrones.com。我正在尝试使用的链接位于顶部栏。

我知道关于这个主题已经有很多主题,但我向你保证我已经阅读过它们(或者至少绝大多数)。他们中的大多数只是一个索引文件没有额外的链接,其余的说这是一个权限问题。所以,如果有人有任何想法,我将非常感激。

python html flask
1个回答
2
投票

事实证明,使用主机PythonAnywhere,您必须单击其页面上的“重新加载站点”按钮,以便传播Flask python文件中的更改。我不知道是这种情况,因为自从我开始将它们用作主机后我没有对应用程序文件进行任何更改。由于HTML,CSS和Javascript的更改在没有重新加载的情况下传播,我从未意识到这一事实。希望如果你有这个问题,你会看到这篇文章,然后浪费3天就像我做的那样...:P

© www.soinside.com 2019 - 2024. All rights reserved.