【问题标题】:I cannot show images which is url type我无法显示 url 类型的图像
【发布时间】:2018-03-22 18:30:29
【问题描述】:

我无法显示 url 类型的图像。 我写在product.html

<body>
{% for product in products.all %}
    <h4> {{ product.product_title }}</h4>
    <img src="{% static product.image }} %}"/>
    <p> {{ product.body }} </p>

{% endfor %}
</body>

在models.py中

class Product(models.Model):
    product_title = models.CharField(max_length=250)
    image = models.TextField()
    body = models.TextField()

在views.py中

def product(request):
    products = Product.objects.order_by('product_title')
    return render(request, 'registration/product.html',{'products':products})

从管理站点,我添加了 product_title&image&body。 图片的网址是

<a href="https://xxxxxx" target="_blank" rel="nofollow">
<img border="0" width="120" height="60" alt="" src="https://wwwyyyyyyyyyyyy"></a>
<img border="0" width="1" height="1" src="https://wwwzzzzzzzzzz" alt="">

通过使用谷歌控制台,加载资源失败:服务器响应状态为 404(未找到)在控制台中。 在 Elements 中,img 标签就像

<img src="/static/wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww">

如何显示图片?img tag & src的方式我写错了吗?

【问题讨论】:

  • 上传的图片不是静态文件。
  • @DanielRoseman 我听不懂你在说什么。你能告诉我更多细节吗?

标签: python html django


【解决方案1】:

如果你想在 Django 中使用图片,你必须像这样配置你的应用程序:

第一步:静态和媒体

在你的 settings.py 文件中,写下这样的内容:

STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, "static")]

PROJECT_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = os.path.join(PROJECT_DIR, 'static/')

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join('path_to_your_Media_directory')

第二步:在模型中实现 Image 字段

在你的 models.py 文件中:

Image = models.ImageField(upload_to='upload_file_directory', null=True, blank=True, width_field=None, height_field=None, default=" ")

第三步:处理视图中的图像

在您的 views.py 文件中(不要忘记 request.FILES or None !):

if request.method == 'POST':

        form = 'YourForm'(request.POST or None, request.FILES or None)

        if form.is_valid() :
            post = form.save()

在您的模板中,不要忘记表单类中的enctype="multipart/form-data"

第四步:在模板中调用图片

{% if 'your_image_exist' %}
   <img src='{{Model.Image.url}}' height="300" width="400"/>
{% endif %}

这是一个全局示例,但是您必须设置所有这些内容才能使用 Django 处理图片! 祝你好运

【讨论】:

  • 谢谢你的回答。你写了 Image = models.ImageField(upload_to='upload_file_directory', null=True, blank=True, width_field=None, height_field=None, default=" ") ,我不能了解如何编写 upload_to='upload_file_directory' 因为我从 Django 管理站点更新图像。
  • 在我的媒体目录中,我有一些目录:picturesmaps,...如果您在 settings.py 中正确配置了您的Media,您只需编写:@ 987654331@ 如果你想引用 Media 中的图片目录 ;) 这样可以上传图片并将这些图片存储在你想要的地方。
【解决方案2】:

您的图片不是静态文件。由于它是一个文本字段并且它的内容是一个 url,因此您只需将值放在 src 属性中。不需要使用静态标签。

试试这个:

 <img src="{{ product.image }}"/>

【讨论】:

  • 可能需要在那里使用|safe 过滤器。
  • thx,你的答案。我写了你的代码,但没有显示图像。通过使用谷歌控制台,[弃用]资源请求,其 URL 包含两个删除的空格(\n\r,@ 987654326@) 个字符和小于号字符 (&lt;) 被阻止。请从元素属性值等位置删除换行符并编码小于字符,以便加载这些资源。有关详细信息,请参阅chromestatus.com/feature/5735596811091968。显示。在我的网站上发生了什么?
  • 看起来product.image 可能包含无效的网址。领域的内容是什么?它是否包含 html 标签,而不仅仅是图片 url?
猜你喜欢
  • 2016-06-28
  • 1970-01-01
  • 2019-07-08
  • 1970-01-01
  • 1970-01-01
  • 2018-11-26
  • 1970-01-01
  • 1970-01-01
  • 2019-04-03
相关资源
最近更新 更多