Reputation: 77
I am building a catalog app using flask and sqlalchemy but one of my routes is giving a 404 and I'm not sure why. Here is the code for the route and function:
@app.route('/Category/<string:category_name>/Item/new', methods=['GET', 'POST'])
def newItem(category_name):
user = getUser()
if user is None:
return redirect('/login')
if request.method == 'POST':
newItem = Category(name=request.form['name'],
description=request.form['description'], creator=session['username'],
category_id=db.query(Category.id).filter_by(category_name=category_name).all())
db.add(newItem)
db.commit()
return redirect(url_for('showCategory(category_name)'))
else:
return render_template('newItem.html', categories=categories)
The newItem.html template does exist in the template folder, and using another route I have for viewing an item: @app.route('/Category/<string:category_name>/Item/<string:item_name>
works so category_name is being defined. The url I'm going to is https://localhost:5000/Category/Sports/Item/new
and Sports is a category in the database, I cannot figure out why I'm getting the 404
Traceback (shows trying to enter showItem route not newItem route):
Upvotes: 0
Views: 2730
Reputation: 9977
Your URL rules are hitting the wrong function...
@app.route('/Category/<string:category_name>/Item/<string:item_name>
happens to match
https://localhost:5000/Category/Sports/Item/new
You need to add an extra segment to the first one like
@app.route('/Category/<string:category_name>/Item/existing/<string:item_name>
Upvotes: 2
Reputation: 6657
You're getting sqlalchemy.orm.exc.NoResultFound exception. It means that view function was called successfully, but now data could be found.
Upvotes: 0