Reputation: 1892
I have a thirdparty app (let's call it app A) that, in its views.py
, it uses Context processors for sending data to specific urls. The data it sends is used in its templates to determine how the nav-bar is like. For example if there exists an A.project
entry in the db, it will show the <i> Projects </i>
in it's template.
Now I'd like to extend that app, and use the nav-bar it uses but add an extra parameter blog
to it where the blog app is a thirdparty app. The problem is that now whenever you go to the url associated with the blog app, e.g. (/blog
), Any items from app A in the nav-bar will be missing because the context sent from the blog app is different and missing data from app A.
I can probably create custom template tags to check if A.project
, etc exist, but I'm not sure if that's really the best way to do it.
Is there any better way of doing it?
Upvotes: 1
Views: 256
Reputation: 948
If all you're looking for is to have some hints weather some data or some apps exist at template-render time, you could use a template context processor, as this is what they're for - loading something into every template.
I definitely wouldn't recommend implementing template tags to retrieve data, this would break the MVC rules for once, but then you might get in trouble while trying to debug slow db queries and other things like that.
If you're doing some db queries in the context processor, bear in mind that those will be executed every time a template is rendered, even if it doesn't need that data.
To shave some time of that processing, you could use some sort of manual caching with an appropriate invalidation scheme.
An alternative route if you are using class based views is to implement a mixin that will just add the data you need to the context (in the get_context_data
method). If you're doing this, make sure to call super
to also get the context of the class base view you're normally extending.
Upvotes: 1