【问题标题】:Show Uploaded File Django: Foreign Key显示上传的文件 Django:外键
【发布时间】:2015-09-28 20:08:25
【问题描述】:

如何在Django 1.8.4的django模板中显示上传的ProductPhoto图片?

适用于以下型号:

class Product(models.Model):
    title = models.CharField(max_length=255, null=True, blank=True)
    description = models.TextField(null=True, blank=True)

    def __str__(self):
        return self.title
    def photos(self):
        return ProductPhoto.objects.all()

class ProductPhoto(models.Model):
    photo = models.ImageField(upload_to='img/product/', null=True, blank=True)
    product = models.ForeignKey(Product, related_name='photos')

使用这些设置:

STATIC_ROOT = '/path/to/static/'
STATIC_URL = '/static/'

MEDIA_ROOT = '/path/to/media/'
MEDIA_URL = '/media/'

还有这些网址:

from django.conf.urls import include, url
from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin

urlpatterns = [
    url(r'^$', 'myApp.views.index', name='index'),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

如何在此模板中显示上传的ProductPhoto 对象:

{% for product in product_list %}
    <div id="some-div">
        {% for photo in product.photos.all %}
        <div class="item"><img src="{{ MEDIA_URL }}{{ photo }}"/></div>
        {% endfor %}
    </div>
{% endfor %}

我以为我在正确的轨道上:{{ MEDIA_URL }}{{ photo }},但这是不正确的。我也尝试过{{ MEDIA_URL }}{{ photo.url }},但模板上都没有显示任何内容。有人能指出我正确的方向吗?

感谢您的想法。

【问题讨论】:

    标签: python django static django-templates media


    【解决方案1】:

    首先,删除照片模型上的 photos() 方法,因为它可能会被误解为 ProductPhoto 对象上的 related_name。

    然后,只需执行以下操作:

    {% for product in product_list %}
        <div id="some-div">
            {% for product_photo in product.photos.all %}
            <div class="item"><img src="{{ product_photo.photo.url }}"/></div>
            {% endfor %}
        </div>
    {% endfor %}
    

    希望对你有帮助,

    【讨论】:

      【解决方案2】:

      以下是您需要更改的内容:

      class Product(models.Model):
          title = models.CharField(max_length=255, null=True, blank=True)
          description = models.TextField(null=True, blank=True)
      
          def __str__(self):
              return self.title
      
          # this is wrong, get rid of it:
          #def photos(self):
          #    return ProductPhoto.objects.all()
      
      class ProductPhoto(models.Model):
          photo = models.ImageField(upload_to='img/product/', null=True, blank=True)
          product = models.ForeignKey(Product, related_name='photos')
      

      你不需要 MEDIA_URL,django 开发服务器知道如何处理它,(在生产中你需要配置 apache、nginx 等来为它们提供服务):

      urlpatterns = [
          url(r'^$', 'myApp.views.index', name='index'),
      ] # + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
      

      就像这样:

      {% for product in product_list %}
          <div id="some-div">
              {% for product_photo in product.photos.all %}
                  <div class="item"><img src="{{ product_photo.photo.url }}"/></div>
              {% endfor %}
          </div>
      {% endfor %}
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-12-08
        • 2016-07-12
        • 2017-03-23
        • 2011-02-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多