【问题标题】:loading an image using the static tag from a static path使用静态标签从静态路径加载图像
【发布时间】:2016-11-27 16:08:20
【问题描述】:

我无法使用以下语法在我的索引页面上加载图像

{% extends "base.html" %}
{% load crispy_forms_tags %}
{% block content %}

    <-!---This is the statement-->
   <-div data-src=  "{% static "bootstrap/images/slider/slider-img1.jpg" %}">
   <-/div>

{% endblock content %}

我在 div 中添加了一个额外的- (dash),因为帖子显示有困难。

无论如何我都会收到错误

Invalid block tag on line 69: 'static', expected 'endblock'. Did you forget to register or load this tag?

我认为这是一个目录结构。我的目录目前看起来像这样

|_mainApp
|  |_______index.html ------>This is where i am trying to run the statement
|_templates
|  |_______base.html ------->The statement works fine in the base
|_static
   |_bootstrap
|_CenterApp
   |_settings.py
   |_urls.py

关于为什么我收到该错误的任何建议是位置问题并且找不到内容?还是别的什么?

【问题讨论】:

  • 在尝试使用static 模板标签之前,您需要在模板顶部添加{% load 'static' %}
  • 是的,谢谢。你能把它作为答案吗
  • 在我这边,关于“静态”的引用太多了。但它只适用于静态。谢谢。

标签: django tags block static-content


【解决方案1】:

使用

  {% load staticfiles %} 

下面你已经扩展了你的 base.html

在 django 模板中,每当您在模板中使用静态时,调用 {% load staticfiles %} 是很重要的。更多详情here

【讨论】:

    【解决方案2】:

    当您使用{% extend "some_page.html" %} 并且需要一些带有静态网址的链接时,您可以使用{{STATIC_URL}} 而不是{% static "/some/link/" %}

    因此,在您的情况下,将其用作:

    <-div data-src=  "{{STATIC_URL}}bootstrap/images/slider/slider-img1.jpg">
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-26
      相关资源
      最近更新 更多