-1

My Flask application gives the error message

Method Not Allowed

The method is not allowed for the requested URL.

After posting a form to update my table

I've tried removing and modifying my POST and GET methods, that has not worked

I've also tried re-engineering the solution that has not worked either.

Python:

import sqlite3    
from flask import Flask, render_template, redirect, request

app = Flask(__name__) 

@app.route('/')
def index():
    connection = sqlite3.connect('suburb.db')
    # create a database cursor
    cur = connection.cursor()
    # query the database for ALL data in the notes table
    cur.execute('SELECT Library_id, Suburb FROM library') #return all resluts from the table employee
    records = cur.fetchall() #fetches all the records (rows) of a query result.
    cur.close()# close the cursor connection
    return render_template('Front_End.html', records=records)

@app.route('/subject', methods=['GET', 'POST'])
def get_subject():
    if request.method == 'POST':
        subject = request.form.get('subject')
        connection = sqlite3.connect('suburb.db')
        cur = connection.cursor()
        print("I have connected to the database bruh") # Fixed typo in "print" statement
        if subject == 'Schools':
            cur.execute('SELECT School_id, Suburb FROM schools')
        elif subject == 'Libraries':
            cur.execute('SELECT Library_id, Suburb FROM library') # Fixed table name
        elif subject == 'Parks':
            cur.execute('SELECT Park_id, Suburb FROM parks')
        elif subject == 'Hotspots':
            cur.execute('SELECT Hotspot_id, Suburb FROM hotspots')
        records = cur.fetchall()
        cur.close()
        return render_template('Front_End.html', records=records, subject=subject)
    else:
        return render_template('Front_End.html')

if __name__=="__main__":
    app.run()

Back End Html

<html>
<body>
<h1>Amenities</h1>

<ul>
    <li>Schools</li>
    <li>Libraries</li>
    <li>Parks</li>
    <li>Hotspots</li>
    {% For key,value in get subjects.item()%}
    <li>{{ key }}</li>
    <li> {{ value }}</li>
    {% endfor%}
</ul>


</body>
</html>

Front End HTML

<html>
  <head>
    <title>CSS Template</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <style>
    * {
      box-sizing: border-box;
      font-family: Arial, Helvetica, sans-serif;
    }
    
    body {
      margin: 0;
      font-family: Arial, Helvetica, sans-serif;
    }
    
    /* Style the top navigation bar */
    .topnav {
      overflow: hidden;
      background-color: #333;
    }
    
    /* Style the topnav links */
    .topnav a {
      float: left;
      display: block;
      color: #f2f2f2;
      text-align: center;
      padding: 14px 16px;
      text-decoration: none;
    }
    
    /* Change color on hover */
    .topnav a:hover {
      background-color: #ddd;
      color: black;
    }
    
    /* Style the content */
    .content {
      background-color: #ddd;
      padding: 10px;
      height: 200px; /* Should be removed. Only for demonstration */
    }
    
    /* Style the footer */
    .footer {
      background-color: #f1f1f1;
      padding: 10px;
    }
    </style>
    </head>
    <div class="topnav">
      <a href="#">Link</a>
      <a href="#">Link</a>
      <a href="#">Link</a>
    </div>

    <style>
        table {
          border-collapse: collapse;
          width: 100%;
          font-family: arial;
        }
        
        th, td {
          text-align: center;
          padding: 15px;

        }
        th {
          background-color: #04AA6D;
          color: white;
        }

        

        tr:nth-child(even) {background-color: #f2f2f2;}
        tr:hover {background-color: #D3D3D3;}
        
        
        h1 {font-family: arial;
        }
        
        </style>
<body>
<div class="content">

    <h2>Selected Amenities</h2>
    <p>What amenities would you like?</p>

    <form method="POST",action="/subject", href="">
      <label for="subject">Select subject:</label>
      <select id="subject" name="subject">
        <option value="Schools">Schools</option>
        <option value="Libraries">Libraries</option>
        <option value="Parks">Parks</option>
        <option value="Hotspots">Hotspots</option>
      </select>
      <button type="submit">Filter</button>
    </form>




    <div style="overflow-x:auto;">
      <table>
        <tr>
          <th>Suburb ID</th>
          <th>Suburb</th>
          <th> 
            {% for record in records %}  <!-- record is the list created from the python code. The for loop iterates through each record (row) -->
            <tr>
            {% for value in record %}    <!-- loop iterates through each value in the record list and generates a table cell for each peice of data -->
                <td>{{ value }}</td>
            {% endfor %}
            </tr>
            {% endfor %}
            </table>
            </div>
</div>
</body>
</html>
davidism
  • 121,510
  • 29
  • 395
  • 339
RayTraced
  • 5
  • 1

1 Answers1

-1
  1. Let's better rename 'Front_End.html' into 'front_end.html' or something like that(snake_case)
  2. Try to use https://flask.palletsprojects.com/en/2.2.x/blueprints/#my-first-blueprint with your template folder and register that blueprint (best practice and maybe it fix your problem)
  3. Separate your code from different logic (create few shortcuts for SQL command, few - for connection to db etc.
  4. Separate style.css in static files (https://flask.palletsprojects.com/en/2.2.x/blueprints/#static-files)