我正在开发宠物商店应用程序以获得乐趣。我的应用程序的工作原理是,它显示一个列出所有宠物商店的页面,以及您点击宠物商店时的页面。你看到宠物商店里的所有动物。Django模型和链接之间的图片链接
问题是,我不知道如何将模型中的图片传递到模板中,而无需静态执行,我很抱歉如果这没有意义。
例子。当有人浏览商店时。他会看到所有的动物,例如名字,描述和图片。
我可以将图片上传到我的管理员,但我怎么能渲染画面的背景下进入我的模板
image = models.FileField(upload_to="images/")
我的模型。
from django.db import models
class Store(models.Model):
name = models.CharField(max_length = 20)
number = models.BigIntegerField()
address =models.CharField(max_length = 20)
def __unicode__(self):
return self.name
class Pet(models.Model):
animal = models.CharField(max_length =20)
description = models.TextField()
owner = models.ManyToManyField(Store)
image = models.FileField(upload_to="images/")
def __unicode__(self):
return self.animal
我views.py
from pet.models import Store , Pet
from django.shortcuts import render_to_response ,get_object_or_404
def index(request):
store = Store.objects.all()
return render_to_response ('store.html',{'store':store})
def brazil(request , animal_id):
pet = get_object_or_404(Pet, pk=animal_id)
return render_to_response ('animal.html',{'pet':pet})
我store.html
Sydney's Pet Store
{% if store %}
<ul>
{% for a in store %}
<li><a href ="{% url world:brazil a.id %}">{{ a.name }}</li>
{% endfor %}
</ul>
{% endif %}
我animal.html
{% if store %}
<li>Pet = {{ store.animal }}</li>
<li>description = {{store.description}} </li>
<li>owner = {{ store.owner }} </li>
{% endif %}
如果这个问题没有意义,我会删除它 – donkeyboy72 2013-03-03 08:24:21
尝试 – catherine 2013-03-03 08:29:40