Django Combine two templated from different apps
Django Combine two templated from different apps
I have two apps with the following structure
project->
main app->
templates->
dashboard.html
my app->
templates->
mydashboard.html
I want to include mydashboard
into dashboard
, well this is possible using include template tag. but my problem appears when I have to pass some parameters into mydashboard
, let's say they are named param1
and param2
these parameters are the variables which I have to load from my app
app. well one possible method is to filling these params in dashboardview in mainapp
and pass them using include
tag into mydashboard.html
like below
mydashboard
dashboard
mydashboard
param1
param2
my app
mainapp
include
mydashboard.html
def user_dashboard(request):
...-->here I have to get data from my app (this view is in main app and I do not want to make main app be dependent to my app
return render(request, 'dashboard.html', 'param1': 0, 'param2': 34)
then in dashboard.html
I add this part
dashboard.html
% is_app_installed "myapp" as is_myapp_installed %
% if is_myapp_installed %
% include "myappdashboard.html" with param1=param1 param2=param2 %
% endif %
It seems the above method works but the main problem is that using this method mainapp
is dependent to myapp
and I do not want to this happens.
is there any other method to load those param1 and param2 inside myapp
?
thanks
mainapp
myapp
myapp
Its already dependent on the
my app
, since you are using the template from other app.– spiritsree
Sep 16 '18 at 19:51
my app
1 Answer
1
I can't see the details of the templates, but if you want to decouple the two templates I suggest you to rethink how you organise your templates logic. Try to rearrange the logic adding a base_dashboard.html
in a common template dir with snippets and base templates to share between the apps.
base_dashboard.html
Then the myappdashboard.html
and dashboard.html
can extend it with % extend base_dashboard.html %
.
myappdashboard.html
dashboard.html
% extend base_dashboard.html %
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy
The only way I can figure would be to have an API in myapp that allows javascript to fetch the data from inside your myappdashboard template
– dirkgroten
Sep 16 '18 at 19:39