0

好的,我知道有一千个人问过这个问题,但我在这个网站上到处查看都没有成功(也是谷歌)这是我的 models.py

VENUE_IMAGE_PATH = os.path.join('images', 'venue_profiles/%Y/%m/%d')

 class Venue(models.Model):

    .....................
   name = models.CharField(max_length=100)
   address = models.CharField(max_length=100)
   city = models.CharField(max_length=100)
   ...........................

class VenueImage(models.Model):
  venue = models.ForeignKey(Venue, related_name="venue")
  image = models.ImageField(upload_to=VENUE_IMAGE_PATH, max_length=255)

这是我的意见.py

def list(request):
  venues = Venue.objects.all()
  images=VenueImage.objects.all()

 return render_to_response('venues/list.html', {'venues':venues,'images':images}, 
  context_instance = RequestContext(request)) 

这是我的模板

{% for v in venues %}

<a href='#'>{{v.name}}</a>
<a href="{% url edit v.id %}" style="position:relative;left:10px;">edit</a>

{% if images %}
<img class='venue_image' src='images/venue_profiles/2012/10/25/{{images.url}}' 
    alt=''>
    {% endif %}
    {% endfor %}

现在我已经尝试过{{images.images.url}}{{images.url}}. {{MEDIA_URL}}images/venue_profiles/%Y/%m/%d/{{image.url}}. 我也试过了{%for i in images %} {{i.url}} {% endfor %}。我也尝试过不使用该'images/venue_profiles/2012/10/25'前缀,但似乎没有任何效果。有人可以帮我看看我做错了什么。

4

1 回答 1

1
# In models.py

class Venue(models.Model):
    ....
    name = models.CharField(max_length=100)
    ....
    images = models.ForeignKey(VenueImage)

class VenueImage(models.Model):
    image = models.ImageField(upload_to=VENUE_IMAGE_PATH)

# In views.py

def list(request):
    venues = Venue.objects.all()
    return render(request,
                  'venues/list.html',
                  {'venues': venues'})

# In template

{% for venue in venues %}
<a href '#'>{{ venue.name }}</a>
...
{% for image in venue.images %}
<img class='venue_image' src=''{{ image.url }}' alt=''>
{% endfor %}
{% endfor %}
于 2012-10-31T05:42:44.433 回答