0

I'm working with Flask 1.0.2 and I'm trying to route the two URLs to the same function.

My two URLs that I want to use the same function are:

/alerts/<string:first_property>/<string:first_value>/<string:manufacturer_value>
/alerts/<string:first_property>/<string:first_value>/<string:manufacturer_value>/<string:category_value>

I have tried setting the value of category_value to None in the function, but when trying to access the first URL I receive this error.

    filters['Category'] = category_number[category_value]
KeyError: None

Full code:

@app.route('/alerts/<string:first_property>/<string:first_value>/<string:manufacturer_value>', methods=['GET'])
@app.route('/alerts/<string:first_property>/<string:first_value>/<string:manufacturer_value>/<string:category_value>', methods=['GET'])
def category(first_property,first_value, manufacturer_value,category_value=None):
    kpi = 'kpi'
    hanaview = 'hanaview'
    filters = {'CalYear': 2015, 'CalQuarter': (1, 3)}
    filters['Category'] = category_number[category_value]
    #print(category_number[category_value])
    filters['ManufacturerDescription'] = manufacturer_value
    progression = ['CategoryDescription','SubcategoryDescription']
    if first_property == 'Hub':
        progression.insert(0,'Country')
    if first_property == 'Division':
        progression.insert(0, 'Hub')
    alert_tree = get(kpi, hanaview, filters, progression, first_property, first_value)
    tree = alert_tree.get_tree()
    return jsonify(tree), status.HTTP_201_CREATED
russianmax
  • 157
  • 1
  • 12

1 Answers1

1

The error you're getting is unrelated to flask routing. You're doing flask routing correctly. The KeyError is coming because your category_number variable doesn't have a value equal to None as its key. I would recommend you use:

filters['Category'] = category_number.get(category_value)

This will default to None if it doesn't find a particular entry in the category_number dictionary.

shashwat
  • 992
  • 1
  • 13
  • 27