Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 显示上载的文件Django:外键_Python_Django_Static_Django Templates_Media - Fatal编程技术网

Python 显示上载的文件Django:外键

Python 显示上载的文件Django:外键,python,django,static,django-templates,media,Python,Django,Static,Django Templates,Media,如何在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 se

如何在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 %}
{%用于产品列表中的产品%}
{%用于product.photos.all%中的照片}
{%endfor%}
{%endfor%}
我认为我的方法是正确的:
{{MEDIA\u URL}{{photo}
,但这是错误的。我也尝试过
{{MEDIA_URL}}{{photo.URL}}
,但都没有在模板上显示任何内容。有人能给我指出正确的方向吗

感谢您的想法。

首先,删除照片模型上的photos()方法,因为它可能会与ProductPhoto对象上的相关名称产生误解

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

{% 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 %}
{%用于产品列表中的产品%}
{product.photos.all%}
{%endfor%}
{%endfor%}

希望有帮助,

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

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')
您不需要媒体URL,django开发服务器知道如何使用它(在生产过程中,您需要配置apache、nginx等来提供这些服务):

就这样,

{% 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 %}
{%用于产品列表中的产品%}
{product.photos.all%}
{%endfor%}
{%endfor%}
{% 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 %}