What's the most efficient way to render a template that changes based on primary key of a table (Django) -


i creating search page users can search parameters. search parameters change based on category searching in. there approximately 100 categories , each has own unique search parameters.

currently have if statement in view checks 100 categories , renders 1 of 100 corresponding forms, feel it's not efficient. efficent/best way this?

currently think this

if category.id == 1:     # render form1 elif category.id == 2:     # render form2 elif category.id == 3:     # render form3 .... 

i'm gonna give answer here may simplify problem little. if have 100 forms , need code them all. maybe may declare sort of global dictionary or array , @ end of each form definition, include them in it, example:

# dict way category.id -> form class form_storage = {}   form1(forms.form)    #fields    #methods form_storage[1] = form1  form2(forms.form)    #fields    #methods form_storage[2] = form2  ... 

then in views might receive get parameter category needs rendered instead of:

if category.id == 1:     # render form1 elif category.id == 2:     # render form2 elif category.id == 3:     # render form3 .... 

you can like:

   # render form    form = form_storage[category.id]() 

and there have reduced views in hundred or more lines of code.

this simplified version of problem, quite possible can done more improvements, specially in declaration of hundred forms.

hope bring light.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -