Изменение URL фляги не регистрируется как ошибка типа и перенаправление 404 не работает - PullRequest
0 голосов
/ 02 ноября 2019

У меня возникла проблема с моим приложением-флягой, когда одна из моих страниц не работает с моим перенаправлением 404 из-за того, что я не зарегистрирован как TypeError.

Вот маршруты. код для того, который работает, где я использовал попытку и за исключением того, чтобы заставить его перейти на страницу 404:

@app.route('/games/<game>')
def game(game):
    conn = sqlite3.connect("retro_games.db")
    cur = conn.cursor()
    cur.execute('''SELECT name, details, image, developerid, categoryid FROM
                Games WHERE name=?''', (game,))
    results = cur.fetchone()
    #if URL doesn't match, serves 404.html page instead of erroring
    try:
        cur.execute("SELECT name FROM Developer WHERE id=?", (results[3],))
        developer = cur.fetchone()
    except TypeError:
        abort(404)

    try:
        cur.execute("SELECT name, details FROM Category WHERE id=?",
                    (results[4],))
        category = cur.fetchone()
    except TypeError:
        abort(404)

    cur.execute('''SELECT name from Console INNER JOIN GamesConsole ON
                Console.id=GamesConsole.cid
                WHERE gid=(select id FROM Games
                           WHERE name=?)''', (game,))
    console = cur.fetchone()
    fetch = cur.fetchone()
    return render_template("show_games.html", page_title='{}'.format(game),
                           results=results, category=category,
                           developer=developer, fetch=fetch, console=console)

А вот HTML-код для этой страницы:

{% extends "layout.html" %}

{% block content %}

<strong><h3>{{results[0]}}</h3></strong>
<div id="showcontent">
  <div id="image">
    <!--provides images for each item/game using the database-->
    <img id="showimg" src='{{results[2]}}' alt="{{results[1]}} Picture">
  </div>
  <!--the following allows each part of the display to use the database to get
  its data by referencing its position in the database-->
  <div class="content">
    <p>{{results[0]}} is {{results[1]}}</p>

  <!--links the developer to the page for said developer for ease of use-->
  <div id="text">
    <h2>Made by: </h2>
  </div>
  <div id="link">
    <a href="/developer/{{developer[0]}}"><p>{{developer[0]}}</p></a>
  </div>

  <div id="text">
    <h2>Category: </h2>
  </div>
    <p>{{category[0]}}: a {{category[1]}}</p>
  </div>

  <div id="text">
    <h2>Console: </h2>
  </div>

  <!--links the console to the page for said console for ease of use-->
  <div id="consoleparatext">
    {{results[0]}} was first released on the
    <div id="link">
      <a href="/console/{{console[0]}}">{{console[0]}}</a>
    </div>
  </div>

</div>

{% endblock %}

Маршрут, в котором возникла проблема с перенаправлением на страницу 404, следующий:

@app.route('/developer/<developer>')
def developer(developer):
    conn = sqlite3.connect("retro_games.db")
    cur = conn.cursor()
    try:
        cur.execute("SELECT name, details, image FROM Developer WHERE name=?",
                    (developer,))
        results = cur.fetchone()
    except TypeError:
        abort(404)

    return render_template("show_developer.html",
                           page_title='{}'.format(developer), results=results)

И HTML:

{%extends "layout.html"%}

{%block content%}
<strong><h3>{{results[0]}}</h3></strong>
<div id="showcontent">
  <div id="image">
    <!--brings in image from database and gives sizes-->
    <img id="showimg" src='{{results[2]}}' alt="{{results[1]}} Picture">
  </div>
  <div class = "content">
    <p>{{results[0]}} was {{results[1]}}</p>
  </div>
</div>
{%endblock%}

Кроме того, вот изображение того, что страницаПохоже, когда URL-адрес изменен и он не перенаправлен на мою страницу 404: Изображение проблемы

Извините за столь длинный пост, любая помощь очень ценится.

...