gpt4 book ai didi

django - Django中如何动态显示图像

转载 作者:行者123 更新时间:2023-12-03 09:19:16 24 4
gpt4 key购买 nike

我正在尝试在 Django 中动态显示图像。这是我的详细信息页面

{% extends 'base.html' %}

{% load staticfiles %}


{% block header %}
<!-- Set your background image for this header on the line below. -->
<header class="intro-header" style="background-image: url('{% static 'blog/img/about-bg.jpg' %}')">
<div class="container">
<div class="row">
<div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
<div class="page-heading">
<h1>{{ post.title }}</h1>
<hr class="small">
<span class="subheading">blog detail</span>
</div>
</div>
</div>
</div>
</header>
{% endblock %}

{% block content %}
<h1>{{ post.title }}</h1>
<h4>{{ post.body }}</h4>
{% lorem 5 p random %}
<hr>

<div id="disqus_thread"></div>
<script>
/**
* RECOMMENDED CONFIGURATION VARIABLES: EDIT AND UNCOMMENT THE SECTION BELOW TO INSERT DYNAMIC VALUES FROM YOUR PLATFORM OR CMS.
* LEARN WHY DEFINING THESE VARIABLES IS IMPORTANT: https://disqus.com/admin/universalcode/#configuration-variables
*/
/*
var disqus_config = function () {
this.page.url = PAGE_URL; // Replace PAGE_URL with your page's canonical URL variable
this.page.identifier = PAGE_IDENTIFIER; // Replace PAGE_IDENTIFIER with your page's unique identifier variable
};
*/
(function() { // DON'T EDIT BELOW THIS LINE
var d = document, s = d.createElement('script');

s.src = '//eights.disqus.com/embed.js';

s.setAttribute('data-timestamp', +new Date());
(d.head || d.body).appendChild(s);
})();
</script>
<noscript>Please enable JavaScript to view the
<a href="https://disqus.com/?ref_noscript" rel="nofollow">comments powered by Disqus.</a>
</noscript>
<script id="dsq-count-scr" src="//eights.disqus.com/count.js" async></script>
{% endblock %}

到目前为止,我尝试存储这些方法。我尝试将其存储在数据库中

 {% static 'blog/img/about-bg.jpg' %}

and called it like this
style="background-image: url('{{ post.title }}')"

那没用。然后我尝试将其存储在数据库中,如下所示

'blog/img/about-bg.jpg'

并这样调用它

style="background-image: url('{% static '{{ post.title }}' %}')

然后我像这样将其存储在数据库中

static/blog/img/about-bg.jpg

并这样调用它

style="background-image: url('{{ post.title }}')"

我也尝试在views.py中定义它

pic = "path/pic.img"
context = {
"pic": pic
context and calling it
{{pic }}

这些方法都不起作用。它与 Laravel 略有不同。在 Laravel 中

path/{{ post->title }} 

会起作用的。我怎样才能在 Django 中做到这一点?欢迎任何和所有建议。需要明确的是,我希望我的所有文章都在索引页面上显示图像,然后当我单击其中一篇文章时,我会进入显示该特定文章的图像的详细信息页面

最佳答案

我已经弄清楚了。它应该存储为

/static/blog/img/about-bg.jpg

不是

static/blog/img/about-bg.jpg

正斜杠使其起作用。在 Laravel 中这并不重要

关于django - Django中如何动态显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34513654/

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