I have a django application and what I want to do is change wherever it says "Company id" in my templates. The thing it can be very tedious because I have to make this change in every template which says "Company id". So then I thought I might create another file that can store this entry, which the I can easy custom the company id.
config.py
company_no = "Company id"
This can work in my forms.py file. I can import company_no by saying
forms.py
from mmc.config import company_no
But then how can I do the same thing for templates? Importing company_no in a template - is there a way round?
-
No Django expert, but can't you render the template with that variable as a parameter? That is what Flask does.Blender– Blender2011年05月03日 14:16:46 +00:00Commented May 3, 2011 at 14:16
-
@Blender Django does this as well - but I want to change every text in my program which says "Company Id". I think it will work if I could put some values in a config file with I could command every time in my program says "Company Id".Shehzad009– Shehzad0092011年05月03日 14:24:03 +00:00Commented May 3, 2011 at 14:24
3 Answers 3
This is what context processors are for. Define your company name in settings.py, then write a context processor that imports it from there and returns it in a dictionary - it will then be available in every template (as long as you use RequestContext to render the template).
2 Comments
Company Id is not a variable. It basically something I have typed up in many tables as a header. But If want to edit Company Id and change it to Marks company it will be tedious going through each template and making changes. If I could make the changes once it would be helpful.As Blender stated, you need to pass variables like this in as part of the context when you render the template. You might make a dictionary or a namedtuple that has common items stored in configuration loaded in a function.
You should also consider using template inheritance if many templates will be display the same data, then you can have methods that load the pieces of context that go with certain base templates.
3 Comments
Company Id. I want to create some sort of file that I can store this label in another file where then if I need to make any changes I could make changes quicker by editing some configuration file.You could create a shared template and use include to load it in to the main template. Then in the shared template you could load and call a custom template tag that produces a context variable and render it as usual.
Alternatively, you could create a custom context processor that loads the data automatically in to the context instance and then render it as usual in the shared template.