django - Django 结合来自不同应用程序的两个模板
问题描述
我有两个具有以下结构的应用程序
project->
main app->
templates->
dashboard.html
my app->
templates->
mydashboard.html
我想包含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})
然后在 dashboard.html
我添加这部分
{% is_app_installed "myapp" as is_myapp_installed %}
{% if is_myapp_installed %}
{% include "myappdashboard.html" with param1=param1 param2=param2 %}
{% endif %}
似乎上述方法有效,但主要问题是使用此方法mainapp
取决于myapp
并且我不希望发生这种情况。还有其他方法可以在里面加载那些 param1 和 param2myapp
吗?谢谢
解决方案
我看不到模板的详细信息,但是如果您想解耦这两个模板,我建议您重新考虑如何组织模板逻辑。尝试重新排列逻辑base_dashboard.html
,在公共模板目录中添加一个片段和基本模板以在应用程序之间共享。
然后myappdashboard.html
和dashboard.html
可以用 扩展它{% extend base_dashboard.html %}
。
推荐阅读
- c++ - 我遇到了错误的访问问题
- user-permissions - 重建关系表与其他表的关系是否合理?
- java - 将附件上传到 azure devops [REST API]
- wpf - 如何以编程方式从 DataGridCheckBoxColumn 获取绑定表达式
- sql - 错误是期望的列数少于返回的列数
- google-sheets - Google表格中的查询公式未评估整个列
- flutter - Flutter:当我热重载我的提供者时创建新对象
- python - 这如何适用于 Python 中的乘法?
- javascript - 如何在不停止 Python 函数的情况下向 JS 发送返回值
- node.js - Puppeteer:仅为主页域添加基本身份验证标头,不适用于第 3 方请求