Quantcast
Channel: Active questions tagged html - Stack Overflow
Viewing all articles
Browse latest Browse all 67411

Flask URL change not registering as a TypeError and 404 redirect not working

$
0
0

I am having a problem at the moment with my flask app where one of my pages isn't working with my 404 redirect as a result of not being registered as a TypeError.

Here is routes.py code for the one that works, where I have used a try and except to force it to go to a 404 page:

@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)

And here is HTML for that page:

{% 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 %}

The route that is having the problem with redirected to a 404 page is this one:

@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)

And the 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%}

Also, here is an image of what the page looks like when the URL is changed and it is not redirected to my 404 page: Picture of problem

Sorry for such a long post, any help is much appreciated.


Viewing all articles
Browse latest Browse all 67411

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>