【问题标题】:using static images with django在 django 中使用静态图像
【发布时间】:2015-05-19 08:03:48
【问题描述】:

我正在尝试将静态图像上传到我的 django 网站。我已经通过了 settings.py 并且所有内容都在那里,应该是因为静态 css 有效。我的问题是我如何对其进行编码,以便在显示产品 id 1 时显示此特定图片。我尝试在下面对其进行编码,但图像没有显示出来。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
{% load staticfiles %}
<head>
    <meta http-equiv="content-type" content="text/html;charset=utf-8" />
    <title> The Book Cove </title>
    <link rel="stylesheet" type="text/css" href="{% static 'screenstyle.css' %}"/>
</head>

<body>

<div id = "content">

<div id = "header"><p class="heading"> The Book Cove.</p></div>

<div id="navbar">
  <ul id="nav">
    <li><a href="Homepage.html" id="current">Homepage</a></li>
    <li><a href="{% url 'fiction_list' %}">Non-Fiction</a></li>
    <li><a href="{% url 'nonfiction_list' %}">Fiction</a></li>
    <li><a href="contact.html">Contact Us</a></li>
  </ul>
</div>

<div id = "titleone"><h1> 
              {% if NonFiction %} Fiction
                  {% elif Fiction %} Non-Fiction
                          {% endif %} 
</h1></div>

<div id = "productone">
{% if Fiction %}
<ul>

    {% for product in Fiction %}
    <li id="pname"><a href=" url 'product_detail' product.id%}">{{ product }}</a></li>
    <p class="price"> £{{ product.price }}</p>
    <p class="author"><em> {{ product.author }} </em></p><hr />
        {% if pk.id = 1 %}
        <img src="{% static 'cat1.png' %}" alt="Fault in our stars" />
        {% elif product.id = 2 %}
        <img src="{% static 'cat2.png' %}" alt = "Looking for alaska" />
        {% elif product.id = 3 %}
        <img src="{% static 'cat3.png' %}" alt = "Looking for alaska" />
        {% elif product.id = 4 %}
        <img src="{% static 'cat4.png' %}" alt = "Looking for alaska" />
        {% elif product.id = 5 %}
        <img src="{% static 'cat5.png' %}" alt = "Looking for alaska" />
        {% endif %}

{% endfor %}
</ul>

{% elif NonFiction %}

<ul>

    {% for product in NonFiction %}
        <li id="pname"><a href=" url 'product_detail' product.id%}">{{ product }}</a></li>
    <p class ="price"> £{{ product.price }}</p>
    <p class ="author"><em> {{ product.author }} </em></p><hr />

    {% endfor %}
</ul>

{% else %}
<p> No Products found!</p>
{% endif %}
</div>

<div id = "footer"><p><strong>Copyright © 2011 The Book Cove. All Rights Reserved</strong></p></div>

</div>

</body>
</html>

【问题讨论】:

    标签: html css django sqlite static


    【解决方案1】:

    您需要使用相等 (==) 而不是赋值 (=)

    {% if pk.id == 1 %}
    

    但是,更好的方法是在 Product 模型上添加一个 ImageField,然后您的模板会更简单。

    {% for product in Fiction %}
        <li id="pname">
          <a href=" url 'product_detail' product.id%}">{{ product }}</a>
        </li>
        <p class="price"> £{{ product.price }}</p>
        <p class="author"><em> {{ product.author }} </em></p><hr />
        <img src="{{ product.image.url }}" alt="Looking for alaska" />
    {% endfor %}
    

    【讨论】:

      猜你喜欢
      • 2013-07-31
      • 1970-01-01
      • 2013-07-27
      • 1970-01-01
      • 2015-07-17
      • 1970-01-01
      • 2020-02-17
      • 2017-03-05
      • 1970-01-01
      相关资源
      最近更新 更多