Skip to content
Advertisement

How to create multiple routes dynamically for same function and pass data which is fetched on the basis of Unique ID to single template in FLASK

I am a newbie to FLASK.

I am creating an e-commerce site in flask and I want to create multiple routes dynamically for same function and pass data which is fetched on the basis of Unique ID to single template in FLASK.

Let me explain it.

I have a table in DB as below:

id category slug
1 cat1 slug1
2 cat2 slug2
3 cat3 slug3
4 cat4 slug4

my python code is:

JavaScript

Please let me know if this code will work or not. If no then what should be the logic?

Thanks in advance.

Advertisement

Answer

I think your asking for how to use variables in routes?

JavaScript

Read up more on route variables in the docs

Generating links to all categories

You comment mentions ‘ that variable should be coming from for loop of data fetched from DB’ the best way I can parse that you want, say, a listing of all current categories (like a navigation bar in an online shop)

You might add a second route:

JavaScript

Then in your all_categories.html you would ‘build’ the urls to the first route:

JavaScript

So both routes work together to get that ‘looped data’ from the database

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement