I've been searching all over for a resolution but can't find the cause. I have an html form that posts to an endpoint in flask but for some reason the function in the route is passing nothing into the variables needed to add to the database.
error message
add_classified(title, price, location, description)
TypeError: add_classified() takes 0 positional arguments but 4 were given
Any help would be greatly appreciated
Code
import sqlite3
from flask import Flask, render_template, request, redirect, url_for
app = Flask(__name__)
def get_classifieds():
conn = sqlite3.connect("classifieds.db")
c = conn.cursor()
c.execute("SELECT \* FROM classifieds")
classifieds = c.fetchall()
conn.close()
return classifieds
def add_classified(title, price, location, description):
conn = sqlite3.connect("classifieds.db")
c = conn.cursor()
c.execute("INSERT INTO classifieds (title, price, location, description) VALUES (?, ?, ?, ?)", (title, price, location, description))
conn.commit()
conn.close()
@app.route("/")
def index():
classifieds = get_classifieds()
return render_template("index.html", classifieds=classifieds)
@app.route("/classified/\<int:id\>")
def classified_detail(id):
conn = sqlite3.connect("classifieds.db")
c = conn.cursor()
c.execute("SELECT \* FROM classifieds WHERE id=?", (id,))
classified = c.fetchone()
conn.close()
return render_template("classified_detail.html", classified=classified)
@app.route("/add", methods=\["GET", "POST"\])
def add_classified():
if request.method == "POST":
\# Get form data
title = request.form\["title"\]
price = request.form\["price"\]
location = request.form\["location"\]
description = request.form\["description"\]
# Add classified to database
add_classified(title, price, location, description)
return redirect(url_for("index"))
else:
return render_template("add_classified.html")
if __name__ == "__main__":
app.run()
tried adding the variables (title, price, location, description) to the function like so:
@app.route("/add", methods=["GET", "POST"])def add_classified(title, price, location, description):if request.method == "POST":# Get form datatitle = request.args.get('title')price = request.args.get('price')location = request.args.get('location')description = request.args.get('description')
# Add classified to database
add_classified(title, price, location, description)
return redirect(url_for("index"))
else:
return render_template("add_classified.html")
if __name__ == "__main__":
app.run()
html form in (classified.html)
<form method="post" action="{{ url_for('add_classified') }}">
<label for="title">Title:</label><br>
<input type="text" id="title" name="title"><br>
<label for="price">Price:</label><br>
<input type="text" id="price" name="price"><br>
<label for="location">Location:</label><br>
<input type="text" id="location" name="location"><br>
<label for="description">Description:</label><br>
<textarea id="description" name="description"></textarea><br><br>
<input type="submit" value="Submit">
</form>