gpt4 book ai didi

html - 在 Django 中使用静态图像

转载 作者:行者123 更新时间:2023-11-28 08:14:12 26 4
gpt4 key购买 nike

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

最佳答案

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

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

关于html - 在 Django 中使用静态图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29089847/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com