Django未显示博客文章中的图像
我已经创建了一个DJANGO博客引擎,它接收我的文章,模板将其解析为html标记。链接等。。正在工作,但它不加载图像文件,而是显示“备选方案”。我已经在单独的html文件中尝试过这个标记,但事实并非如此。只是不显示django博客文章中的图像 模板文件的相关部分:Django未显示博客文章中的图像,django,blogs,Django,Blogs,我已经创建了一个DJANGO博客引擎,它接收我的文章,模板将其解析为html标记。链接等。。正在工作,但它不加载图像文件,而是显示“备选方案”。我已经在单独的html文件中尝试过这个标记,但事实并非如此。只是不显示django博客文章中的图像 模板文件的相关部分: {% include 'blog/header.html' %} </aside> <section id ="content"> <article> {%for post in posts %}
{% include 'blog/header.html' %}
</aside>
<section id ="content">
<article>
{%for post in posts %}
<h2><a href="{{ post_get_absolute_url }}">{{ post.title }}</a></h2>
<h3>{{ post.created}}</h3>
<p>
{{ post.body|safe }}<br>
</p>
<h3>By {{ post.author }}</h3>
{%include'blog/header.html%}
{{post.created}}
{{post.body | safe}}
作者{post.author}
我正在复制粘贴有问题的帖子
<---- text here ------------------>
<a href="http://gdcm.sourceforge.net/html/classgdcm_1_1Directory.html">GDCM::Directory</a>
<img src="/home/usman/www/deejay/blog/static/images/dicomdir.png" />
This is it
就是这个
有趣的是,“a”标记可以正常工作,但“img”标记不起作用。我已经尝试了许多变体,但我希望一些内联代码能够显示简单的html标记,当然,我也会通过编程将一些变量从帖子内部传递到模板,让模板知道图像的位置。您的问题就在这里:
{{post\u get\u absolute\u url}
。您应该使用{{post.get\u absolute\u url}
更好的方法是直接调用图像的URL;这样,您就可以在url.py中维护URL,而不是在模型代码中维护URL。它使你的应用程序更加便携
从模型中获取图像字段名称值,然后{{{post.image.url}}
。例如,如果您的模型是:
class Post(models.Model):
img = models.ImageField(upload_to='images')
然后您将使用{{post.img.url}
当我将完整地址替换为
<img src="/static/images/dicomdir.png"/>
在开发服务器和生产服务器上。帮助我的是,我查看了终端上的开发服务器响应,并能够找出url。您可能应该使用{{STATIC\u url}}
标记<代码>
只需确保已正确调整设置即可。