rajpy
rajpy

Reputation: 2476

Routing three URLs to single endpoint ? Using Flask and Jinja2

I need to map three URLs to single view function.

@app.route('/items/', defaults={"item_name": "all", "status": "available"}, methods=['GET', 'POST'])
@app.route('/items/<item_name>/')
@app.route('/items/<item_name>/<status>')
def items_list(item_name, status):
    --- code goes here ----

First and last URL works. i.e., /items/ - item_name and status will have default values. and /items/some_item_name/unavailable/ - it uses passed values.

When I try /items/some_item_name/ - it fails with this error "TypeError: item_list() takes exactly 2 arguments (1 given)", which I understand as function is expecting two parameters. Is it not the correct way doing it? Why 'status' is not taking default value 'available'. Because when we issue /items/, 'item_name' and 'status' takes default values. I was expecting same thing to happen when /items/some_item_name/ is used.

What is going wrong here? Thanks for any help..

Upvotes: 3

Views: 996

Answers (1)

angvillar
angvillar

Reputation: 1094

Use default arguments in your function:

def items_list(item_name=None, status=None):

Upvotes: 5

Related Questions