I'm creating basic Flask website & API.
I want to delete a record from SQL Server. I managed to work out creating & updating records.
I've came up with the following:
api2.py
import secrets
import pdb
secret = secrets.token_urlsafe(32)
from flask import Flask,render_template,url_for, request, redirect, flash
import pyodbc
app = Flask(__name__)
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SQLALCHEMY_ENGINE_OPTIONS'] =None
app.config['SESSION_TYPE'] = 'memcached'
app.secret_key = secret
conn = pyodbc.connect('Driver={SQL Server};'
'Server=127.0.0.1;'
'Database=AdventureWorksDW2019;'
'UID=sa;'
'PWD=xxx;'
'Trusted_Connection=no;')
@app.route('/', methods= ['GET'])
def home():
cursor = conn.cursor()
cursor.execute("SELECT * FROM notes")
productDetails= cursor.fetchall()
return render_template('home.html',
productDetails=productDetails)
@app.route('/delete/<string:id>', methods= ['DELETE'])
def delete(id):
if request.method=='DELETE':
cursor=conn.cursor()
productDetails1=cursor.execute("DELETE FROM notes WHERE id=?", (id))
conn.commit()
cursor.close()
return redirect('/')
home.html
{% extends 'base.html' %}
{% set active_page = 'home' %}
{% block content %}
<table bolder= 1px>
<tr>
<td> id </td>
<td> note </td>
<td> admin </td>
</tr>
{% for note in productDetails %}
<tr>
<td> {{note[0]}} </td>
<td> {{note[1]}} </td>
<td> <a href='/edit/{{note[0]}}'>Edit</a> <a href='/new_item'>Add</a> <a href='/delete/{{note[0]}}'>Delete</a> </td>
</tr>
{% endfor %}
</table>
{% endblock %} -->
I'm stuck with an error below when I click a link on the generated url in the table:
url: http://127.0.0.1:5000/delete/3
Method Not Allowed The method is not allowed for the requested URL.