In my views.py
, I used to have the following structure:
...
if mymodel.name == 'Myname1':
#do something
elif mymodel.name == 'Myname2':
#do something else
...
However, I found this method cumbersome because if the names change, I would have to search through all my code to make corrections. So, I decided to centralize these words in a separate file:
hardcoded_words.py
:
myname1='Myname1'
myname1='Myname2'
myname1='Myname3'
...
This way, my views.py
was updated to:
from myapp import hardcoded_words
...
if mymodel.name==hardcoded_words.myname1:
#do something
elif mymodel.name==hardcoded_words.myname2:
#do something else
...
If 'Myname1' changes, I only need to correct one file:
hardcoded_words.py
:
...
myname1='Myname1_b'
...
I wonder if there is a better approach for this issue (feel free to share your thoughts). However, my concern lies with JavaScript. Is there a similar way to handle this?
Here’s an example from my javascript.js file:
function myfunction1(myvariable1, myvariable2) {
switch (myvariable1) {
case 'Myname1':
//do something
break;
case 'Myname2':
//do something else
break;
...
Thank you for any insights or suggestions.