【问题标题】:Accessing Django's template folder via /static/js/通过 /static/js/ 访问 Django 的模板文件夹
【发布时间】:2018-12-21 12:57:48
【问题描述】:

在我的一个 Djano 项目中,我在 /project/static/js/ 中有一个 JS 文件。它需要访问存储在/project/templates/ 的文件。我无法从 js 文件访问此位置。

有人可以建议解决方法(在 JS 文件夹中保留上述文件的副本)。我想避免重复(因为它会使我的维护工作加倍)

任务细节:我有一些显示说明的 HTML 模板。我编写的 JS 只是采用相同的指令文本并将其显示为动态弹出窗口上的文本。

注意:主要问题是任何 JS 解决方案都必须存在非 JS 后备。这个项目的一些用户来自一个去除所有<script>标签的正向代理。想想Unobtrusive JS

【问题讨论】:

  • simlink 怎么样?!:ln -s /project/templates/my_file /project/static/js/my_file
  • @jojo,这不是我要做的第一选择。是的,它可以工作,但我不想在操作系统级别做任何事情来解决应用程序的问题。
  • 当然。我也不会这样做。但我想这更像是一个设计问题。我猜最好的解决方案是不需要从静态目录中访问模板文件夹。
  • 你能添加提供模板的视图,然后让你的 js 调用这些视图的 url 吗?
  • @jcfollower: : 我不关注。能多说明一点吗?

标签: python django django-static


【解决方案1】:

无需从static 文件夹访问projects/templates

您有多种选择如何在弹出窗口中显示 HTML 内容,例如说明。

这里概述了两种方法:

第一个选项:一次加载所有内容

这种方法一次性加载所有内容,而 js 只会更改指令弹出窗口的可见性。如果您使用的是 Bootstrap,那么 modal 会让您的生活更轻松。您甚至不需要完全编写任何 js。使用 Bootstrap,这看起来像:

<!-- main_template.html -->
<!-- Don't forget to load the bootstrap library here -->
....
<!-- icon to trigger the popup -->
<a data-toggle="modal" data-target="#InstructionModal">
    <span class="glyphicon glyphicon-info-sign" aria-hidden="true"></span>&nbsp;Show Instructions
</a>
....

<!-- the bootstrap modal -->
<div class="modal fade" id="InstructionModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="myModalLabel">Instructions</h4>
      </div>
      <div class="modal-body">
        {% include 'instruction.html' %}
      </div>
    <div class="modal-footer">
      <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
    </div>
  </div>
</div>

然后是说明:

<!-- instruction.html -->
<p>
  This is an instruction - You must follow it!
</p>

第二个选项:使用ajax 按需加载其他内容

在这种情况下,您不会在开始时加载额外的 HTML 内容,而是仅在请求时提供,即如果有人单击 显示说明 图标。请注意,您需要 jQuery 才能使其工作。

在这里您的说明可以查看(不要忘记更新您的urls.py):

# view.py
def get_instructions(request):
    return render(request, 'instruction.html')

说明模板还是一样的:

<!-- instruction.html -->
<p>
  This is an instruction - You must follow it!
</p>

js:

<!-- get_instructions.js -->
....
<script>
$("#InstroductionModal").on("show.bs.modal", function(e) {
    var link = $(e.relatedTarget);
    $(this).find(".modal-body").load(link.attr("href"));
});
</script>

主模板:

<!-- main_template.html -->
<!-- Don't forget to load get_instructions.js -->
....
<!-- icon to trigger the popup -->
<a href="{% url 'get_instructions' %}" data-remote="false" data-toggle="modal" data-target="#InstructionModal">
    <span class="glyphicon glyphicon-info-sign" aria-hidden="true"></span>&nbsp;Show Instructions
</a>

<!-- the bootstrap modal -->
<div class="modal fade" id="InstructionModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
        <h4 class="modal-title" id="myModalLabel">Instructions</h4>
      </div>
      <div class="modal-body">
        <p> Instructions will follow </p>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">
          Close
        </button>
      </div>
    </div>
  </div>
</div>

希望其中一种方法对您有用。不幸的是,我没有 django 项目设置,所以这段代码未经测试。

【讨论】:

  • 我当然更喜欢使用 jQuery(方法 2),但我的问题是我还需要支持非 JS 客户端。我的一些用户来自一个去除所有&lt;script&gt; 标签的转发代理。这就是导致我的计划最初出现问题的原因。 Think Unobtrusive JS (en.wikipedia.org/wiki/Unobtrusive_JavaScript)
  • @HassanBaig 但是你不能使用 &lt;script&gt; 标签 our/ 并且你想要一个也可以工作的非 js 后备解决方案?!如果你不能使用 script 引导不是一个选项,我猜,或者类似 github.com/intesso/rework-bootstrap 的东西。也许您可以在触发图标弹出窗口中添加target&lt;a href="{% url 'get_instructions' %}" target="_blank"&gt;
猜你喜欢
  • 1970-01-01
  • 2019-04-16
  • 2013-03-02
  • 1970-01-01
  • 1970-01-01
  • 2018-05-26
  • 2017-02-12
  • 2021-11-02
  • 1970-01-01
相关资源
最近更新 更多