0

I want to build some very similar apps in Django, but I want them to load different tag/filter modules. For this I want the html templates to be generic. For example, I would like to have something like

{% load specific_tags %}

and then I would like to define the variable specific_tags in a context processor for each app. But Django thinks that it should look for "specific_tags" in the installed apps in settings.py. How can I tell Django/Python to first evaluate the content of the variable from the context processor?

Best regards

MDoe
  • 163
  • 1
  • 13

1 Answers1

0

Why don't you simply put this values in setting files, this is the way how I am differentiating between dev, test, demo and production environments, and the use one of the solution found here to load that value sin your template:

Can I access constants in settings.py from templates in Django?

Branko Radojevic
  • 660
  • 1
  • 5
  • 14
  • I am not sure that I understand you entirely. Let's say I have three apps A, B, and C. In order to have a code that is easily maintainable, I would like to use the same html templates, but actually inside them there have to be some differences like different paths and so on. So I want to have variables in them, which are replaced according to which app is in the current view. For this I wanted to use different filters. But I would even like to have the same filter name as a variable in each of them so that the template files can be identical. – MDoe Aug 29 '20 at 20:26
  • Well, as I wrote before, you can put everything in your settings.py, especially paths. Similarly you can create new file called fro example texts.py and put all your variables there and include it in your code. Than you can use that in your templates. I don't see need for creating filters here. – Branko Radojevic Aug 30 '20 at 07:40
  • Okay, so I would have a text file like texts.py or variables.py or something like that. And in it I would have something like varpath1 = '/yada/' varpath2 = '/foo/' varpath3 = '/bar' But then in the templates, I would always only want to have something like varpath and not varpath1/2/3. How could I then tell the template to substitute the varpath with the corresponding var from texts.py or variables.py? – MDoe Aug 30 '20 at 08:02
  • If I understood you well, you are making multiple applications, but wish to use same templates, right? So, if this the case, you will have in variables.py of app1 varpath= 'something_app1" and in variables.py of app2 you will have same varpath="something_app2". And in templates you will always use varpath. Is that what you wish to achieve? – Branko Radojevic Aug 30 '20 at 19:10
  • Yes, that is the idea. – MDoe Aug 31 '20 at 20:28
  • I am not sure, but if I do not want to send the same variable to each view in an app by always including it in the corresponding context of each view request, then I guess I have to use a context processor. However, when I use app specific context processors and in them the same variable name for each app (and likewise in the templates), then they will get mixed up. :( – MDoe Aug 31 '20 at 20:34
  • Maybe you should take a loog here: https://stackoverflow.com/a/25841039/6705092 – Branko Radojevic Sep 01 '20 at 07:54
  • Thanks, I will do that. – MDoe Sep 01 '20 at 10:21