使用django的静态图像

时间:2015-03-17 01:27:20

标签: html css django sqlite static

我正在尝试将静态图像上传到我的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>

1 个答案:

答案 0 :(得分:0)

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

{% if pk.id == 1 %}

但是,更好的方法是在ImageField模型上设置Product,然后您的模板会更简单。

{% 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 %}