Search code examples
pythonflaskjinja2werkzeug

Flask: How to fix Build Error when building URL endpoint


I am trying to run my Flask app which was previously working, although now throws the error

werkzeug.routing.exceptions.BuildError: Could not build url for endpoint 'dataVis'. Did you mean 'static' instead?

main.py

#importing flask library
from flask import Flask, render_template, request, redirect, url_for
import dbInteraction as dbi

#creating flask app
app = Flask("Bruh bruh bruh")
app.static_folder = 'static'

#url endpoint for the index/home page, returns the index.html page
@app.route('/')
def index():
    return render_template('index.html')

@app.route('/data-visualisation')
def dataVis():
    return render_template('dataVisualisation.html')

@app.route('/getData', methods=['GET'])
def getData():

    if request.method == "GET":

        dataToReturn = dbi.loadData()

        return dataToReturn

if __name__ == '__main__':
  app.run(host='0.0.0.0', port=81, debug=True)

index.html

<!DOCTYPE html>
<html>
    <body>
        {% extends "pageLayout.html" %}

        {% block pageBody %} 

        IRRELEVALANT HTML CODE

        {% endblock %}
    </body>
</html>

pageLayout.html

<!DOCTYPE html>
<?xml version="1.0" encoding="utf-8"?>
<html>
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>Missing The Target</title>
        <meta name="description" content="">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" href="../static/css/style.css">
        <style>
            @import url('https://fonts.googleapis.com/css2?family=Playfair+Display:wght@500&display=swap');
            @import url('https://fonts.googleapis.com/css2?family=Oswald:wght@500&display=swap');
        </style>
    </head>

    <body>
        <script src="../static/js/currentPageCss.js" async defer></script>
        <script src="../static/js/getData.js" async defer></script>
        <header>
            <h1 class="oswaldFont">Missing The Target</h1>
            <nav>
                <a id="indexLink" class="oswaldFont" href="{{ url_for('index') }}">Home</a>
                <a id="dataVisLink" class="oswaldFont" href="{{ url_for('dataVis') }}">Data Visualised</a>
            </nav>
        </header>
        {% block pageBody %} {% endblock %}
    </body>
</html>

also in regards to the html, I am aware url_for() should be used for accessing the static files, this will be done.

I do not understand why the error is thrown when the method/route is clearly defined as the same name called in url_for()

I'd appreciate any help :)

I have tried removing any invisible characters & changing the method name in the python and the HTML.

When I try access the 'data-visualisation' url manually, i receive a 404 not found error as well as if i switch the html code to url_for('getData'). Due to this I believe the error lies in Flask not reading the methods after index, although am unsure why.


Solution

  • I fixed this issue myself, although aren't entirely sure what the issue was, most likely due to multiple python instances running by accident. If you run in to this issue, the steps I took were:

    1. Kill all python environments through the terminal taskkill /IM python.exe /F (Windows)
    2. Moved the dataVis function and @app.route before the index endpoint in my main.py file
    3. Restarted my computer