Flask won´t run my code and is stuck on index page

53 views Asked by At

I am just finishing this project and i´ve got everything done but eventhough everything seems alright flask server won´t let me through selection on index page :( any ideas? i need this in couple hours for my college proffesor and i am quite desperate. `

from flask import Flask, render_template, request, redirect, url_for
import logging
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(message)s')

app = Flask(__name__)

deniky = {}

@app.route('/', methods=['GET', 'POST'])
@app.route('/index', methods=['GET', 'POST'])
def index():
    if request.method == 'GET':
        return render_template('index.html', deniky=deniky)
    elif request.method == 'POST':
        return redirect(url_for('index'))

@app.route('/test', methods=['GET', 'POST'])
def test():
    if request.method == 'GET':
        return '<form action="/test" method="post"><input type="submit" value="Send" /></form>'
    elif request.method == 'POST':
        return "OK this is a post method"
    else:
        return "ok"

@app.route('/vytvorit_denik', methods=['GET', 'POST'])
def vytvorit_denik():
    if request.method == 'POST':
        nazev_deniku = request.form['nazev_deniku']
        if nazev_deniku not in deniky:
            deniky[nazev_deniku] = []
        return redirect(url_for('index'))

@app.route('/ulozit_denik', methods=['GET', 'POST'])
def ulozit_denik():
    if request.method == 'GET':
        return render_template('index.html', deniky=deniky)
    elif request.method == 'POST':
        nazev_deniku = request.form['nazev_deniku']
        obsah_deniku = deniky.get(nazev_deniku)
        if obsah_deniku:
            with open(f'{nazev_deniku}.txt', 'w') as file:
                file.write('\n'.join(obsah_deniku))
        return redirect(url_for('index'))

@app.route('/nacist_denik', methods=['GET', 'POST'])
def nacist_denik():
    if request.method == 'GET':
        return render_template('index.html', deniky=deniky)
    elif request.method == 'POST':
        nazev_souboru = request.form['nazev_souboru']
        try:
            with open(f'{nazev_souboru}.txt', 'r') as file:
                obsah_deniku = file.read().splitlines()
            deniky[nazev_souboru] = obsah_deniku
        except FileNotFoundError:
            pass
        return redirect(url_for('index'))

@app.route('/vlozit_zaznam', methods=['GET', 'POST'])
def vlozit_zaznam():
    if request.method == 'GET':
        return render_template('index.html', deniky=deniky)
    elif request.method == 'POST':
        nazev_deniku = request.form['nazev_deniku']
        author = request.form['author']
        title = request.form['title']
        genres = request.form['genres'].split(',')  
        publication_year = request.form['publication_year']
        number_of_pages = request.form['number_of_pages']
        read_date = request.form['read_date']
        
        zaznam = {
            'author': author,
            'title': title,
            'genres': genres,
            'publication_year': publication_year,
            'number_of_pages': number_of_pages,
            'read_date': read_date
        }
        
        if nazev_deniku in deniky:
            deniky[nazev_deniku].append(zaznam)
        else:
            deniky[nazev_deniku] = [zaznam]
        return redirect(url_for('index'))

@app.route('/vyhledat_zaznam', methods=['GET', 'POST'])
def vyhledat_zaznam():
    if request.method == 'GET':
        return render_template('index.html', deniky=deniky)
    elif request.method == 'POST':
        nazev_deniku = request.form['nazev_deniku']
        kriterium = request.form['kriterium']
        hodnota = request.form['hodnota']

        if nazev_deniku in deniky:
            zaznamy = deniky[nazev_deniku]
            nalezeny_zaznamy = []

            for zaznam in zaznamy:
                if kriterium in zaznam and zaznam[kriterium] == hodnota:
                    nalezeny_zaznamy.append(zaznam)

            return render_template('zobrazit_vysledky_vyhledavani.html', nazev_deniku=nazev_deniku, nalezeny_zaznamy=nalezeny_zaznamy)

        return redirect(url_for('index'))

@app.route('/smazat_zaznam', methods=['GET', 'POST'])
def smazat_zaznam():
    if request.method == 'GET':
        return render_template('index.html', deniky=deniky)
    elif request.method == 'POST':
        nazev_deniku = request.form['nazev_deniku']
        index_zaznamu = int(request.form['index_zaznamu'])
        
        if nazev_deniku in deniky and 0 <= index_zaznamu < len(deniky[nazev_deniku]):
            del deniky[nazev_deniku][index_zaznamu]
        return redirect(url_for('index'))

@app.route('/smazat_vsechny_zaznamy', methods=['GET', 'POST'])
def smazat_vsechny_zaznamy():
    if request.method == 'GET':
        return render_template('index.html', deniky=deniky)
    elif request.method == 'POST':
        nazev_deniku = request.form['nazev_deniku']
        
        if nazev_deniku in deniky:
            deniky[nazev_deniku] = []
        return redirect(url_for('index'))

@app.route('/smazat_cely_denik', methods=['GET', 'POST'])
def smazat_cely_denik():
    if request.method == 'GET':
        return render_template('index.html', deniky=deniky)
    elif request.method == 'POST':
        nazev_deniku = request.form['nazev_deniku']
        
        if nazev_deniku in deniky:
            del deniky[nazev_deniku]
        return redirect(url_for('index'))

if __name__ == '__main__':
    app.run( host="0.0.0.0", port=8000)

here is python part and this is html part

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Čtenářský deník</title>
    <link href="{{ url_for('static', filename='styles/style.css') }}" rel="stylesheet" />
</head>
<body>
  <h1>Vyberte možnost</h1>
  <form action="/" method="POST">
      <label for="moznost">Vyberte možnost:</label>
      <select id="moznost" name="moznost">
          <option value="vytvorit_denik">Vytvořit deník</option>
          <option value="ulozit_denik">Uložit deník do souboru</option>
          <option value="nacist_denik">Načíst deník ze souboru</option>
          <option value="vlozit_zaznam">Vložit záznam</option>
          <option value="vyhledat_zaznam">Vyhledat záznam</option>
          <option value="smazat_zaznam">Smazat záznam</option>
          <option value="smazat_vsechny_zaznamy">Smazat všechny záznamy</option>
          <option value="smazat_cely_denik">Smazat celý deník</option>
      </select>
      <br>
      <button type="submit">Potvrdit</button>
  </form>
</body>
</html>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Načíst deník ze souboru</title>
    <link href="{{ url_for('static', filename='styles/style.css') }}" rel="stylesheet" />
</head>
<body>
  <h1>Načíst deník ze souboru</h1>
  <form action="/nacist_denik" method="POST">
      <label for="nazev_souboru">Název souboru:</label>
      <input type="text" id="nazev_souboru" name="nazev_souboru"><br>
      <button type="submit">Načíst deník</button>
  </form>
</body>
</html>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Smazat celý deník</title>
    <link href="{{ url_for('static', filename='styles/style.css') }}" rel="stylesheet" />
</head>
<body>
    <h1>Smazat celý deník</h1>
    <form action="/smazat_cely_denik" method="POST">
        <label for="nazev_deniku">Název deníku:</label>
        <select id="nazev_deniku" name="nazev_deniku">
            {% for nazev, _ in deniky.items() %}
                <option value="{{ nazev }}">{{ nazev }}</option>
            {% endfor %}
        </select>
        <br>
        <button type="submit">Smazat celý deník</button>
    </form>
</body>
</html>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Smazat všechny záznamy</title>
    <link href="{{ url_for('static', filename='styles/style.css') }}" rel="stylesheet" />
</head>
<body>
    <h1>Smazat všechny záznamy</h1>
    <form action="/smazat_vsechny_zaznamy" method="POST">
        <label for="nazev_deniku">Název deníku:</label>
        <select id="nazev_deniku" name="nazev_deniku">
            {% for nazev, _ in deniky.items() %}
                <option value="{{ nazev }}">{{ nazev }}</option>
            {% endfor %}
        </select>
        <br>
        <button type="submit">Smazat všechny záznamy</button>
    </form>
</body>
</html>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Smazat záznam</title>
    <link href="{{ url_for('static', filename='styles/style.css') }}" rel="stylesheet" />
</head>
<body>
    <h1>Smazat záznam</h1>
    <form action="/smazat_zaznam" method="POST">
        <label for="nazev_deniku">Název deníku:</label>
        <select id="nazev_deniku" name="nazev_deniku">
            {% for nazev, _ in deniky.items() %}
                <option value="{{ nazev }}">{{ nazev }}</option>
            {% endfor %}
        </select>
        <br>
        <label for="index_zaznamu">Index záznamu:</label>
        <input type="number" id="index_zaznamu" name="index_zaznamu" min="0"><br>
        <button type="submit">Smazat záznam</button>
    </form>
</body>
</html>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Uložit deník do souboru</title>
    <link href="{{ url_for('static', filename='styles/style.css') }}" rel="stylesheet" />
</head>
<body>
    <h1>Uložit deník do souboru</h1>
    <form action="/ulozit_denik" method="POST">
        <label for="nazev_deniku">Název deníku:</label>
        <select id="nazev_deniku" name="nazev_deniku">
            {% for nazev, _ in deniky.items() %}
                <option value="{{ nazev }}">{{ nazev }}</option>
            {% endfor %}
        </select>
        <br>
        <button type="submit">Uložit deník</button>
    </form>
</body>
</html>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Vložit záznam</title>
    <link href="{{ url_for('static', filename='styles/style.css') }}" rel="stylesheet" />
</head>
<body>
    <h1>Vložit záznam</h1>
    <form action="/vlozit_zaznam" method="POST">
        <label for="nazev_deniku">Název deníku:</label>
        <select id="nazev_deniku" name="nazev_deniku">
            {% for nazev, _ in deniky.items() %}
                <option value="{{ nazev }}">{{ nazev }}</option>
            {% endfor %}
        </select>
        <br>
        <label for="author">Autor:</label>
        <input type="text" id="author" name="author"><br>
        <label for="title">Název knihy:</label>
        <input type="text" id="title" name="title"><br>
        <label for="genres">Žánry:</label>
        <input type="text" id="genres" name="genres"><br>
        <label for="publication_year">Rok publikace:</label>
        <input type="text" id="publication_year" name="publication_year"><br>
        <label for="number_of_pages">Počet stran:</label>
        <input type="text" id="number_of_pages" name="number_of_pages"><br>
        <label for="read_date">Datum přečtení:</label>
        <input type="text" id="read_date" name="read_date"><br>
        <button type="submit">Vložit záznam</button>
    </form>
</body>
</html>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Vyhledat záznam</title>
    <link href="{{ url_for('static', filename='styles/style.css') }}" rel="stylesheet" />
</head>
<body>
    <h1>Vyhledat záznam</h1>
    <form action="/vyhledat_zaznam" method="POST">
        <label for="nazev_deniku">Název deníku:</label>
        <select id="nazev_deniku" name="nazev_deniku">
            {% for nazev, _ in deniky.items() %}
                <option value="{{ nazev }}">{{ nazev }}</option>
            {% endfor %}
        </select>
        <br>
        <label for="kriterium">Kritérium:</label>
        <select id="kriterium" name="kriterium">
            <option value="author">Autor</option>
            <option value="title">Název knihy</option>
            <option value="genres">Žánry</option>
            <option value="publication_year">Rok publikace</option>
            <option value="number_of_pages">Počet stran</option>
            <option value="read_date">Datum přečtení</option>
        </select>
        <br>
        <label for="hodnota">Hodnota:</label>
        <input type="text" id="hodnota" name="hodnota"><br>
        <button type="submit">Vyhledat</button>
    </form>
</body>
</html>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Vytvořit deník</title>
    <link href="{{ url_for('static', filename='styles/style.css') }}" rel="stylesheet" />
</head>
<body>
    <h1>Vytvořit deník</h1>
    <form action="/vytvorit_denik" method="POST">
        <label for="nazev_deniku">Název deníku:</label>
        <input type="text" id="nazev_deniku" name="nazev_deniku"><br>
        <button type="submit">Vytvořit deník</button>
    </form>
</body>
</html>

I have tried chatgpt, install everything and followed almost every available advice.

I hope someone knows how to solve this:)

1

There are 1 answers

0
0x00 On

I'm not sure exactly what you want to do but its sending you to the index after selecting an option because thats all your index do. It either render the index or redirects to index.

def index():
    if request.method == 'GET':
        return render_template('index.html', deniky=deniky)
    elif request.method == 'POST':
        return redirect(url_for('index'))

When you submit the form, it sends a POST request to "/" which is your index. Take a look at what your index does with a POST request. (Redirects to index).

Also, all your routes seem to render "index.html" when its a GET request. So you are always going to get the "index" no matter what route you choose.

Try running flask with debug enabled. You'll get a stacktrace instead of 500 http status code. app.run(host="0.0.0.0", port=8000, debug=True)