0

I am creating a web page displaying information concerning items and trying to implement the following structures

 - /ITEMS
 |           + /ITEMS/ITEM_1
 |           + /ITEMS/ITEM_2
 ...

The route /ITEM_LIST contains an overview of all items and each /ITEMS/ITEM_X contains specific information about the given item.

Since my item list is not static, I would like to create dynamically the ITEMS/ITEM_X pages. For this purpose I have a function

def html_table_0(table: str, title: str = 'na'):
    return render_template('table_template.html',
                           tables=[table],
                           titles=['na', title])

This works fine when I have one specific item:

obj_name = obj.name  # (obj is an instance of custom class Obj representing my items)
@app.route('/objects/' + obj_name, methods=("POST", "GET"))
def html_table():
    return render_template('table_template.html',
                           tables=[obj.to_html()],
                           titles=['na', obj.title])

But it fails when trying to do this iteratively.

I tried to make use of this SO post:

counter, routes = 0, [None]*obj_array.__len__()
for obj in obj_array:
    routes[counter] = dict(route='/odds/'+obj.name,
                           func=lambda: html_table(table=obj.to_html()),
                           page=obj.name)

But this throws the exception

AssertionError: View function mapping is overwriting an existing endpoint function: obj.name

Any guidance would appreciated.

user101
  • 476
  • 1
  • 4
  • 9
  • `@app.route('/objects/')` and `def html_tables(object_id)`. This has to be a dupe, I need to find the canonical. In your template you want the link to point to `{{ url_for('html_table', object_id=object_id) }}` where the second `object_id` is passed when you render the tamplate – roganjosh Aug 14 '19 at 10:19
  • why to create many routes if you can create one with parameter. – furas Aug 14 '19 at 10:20
  • Also see [the documentation](https://flask.palletsprojects.com/en/1.0.x/quickstart/#variable-rules) – roganjosh Aug 14 '19 at 10:32
  • @roganjosh I see, thanks! – user101 Aug 14 '19 at 10:34

0 Answers0