我有一個簡單的寵物應用程序。Django No Pet與給定的查詢匹配
該應用程序顯示所有的寵物商店,你點擊寵物商店時。你會看到所有的寵物名稱,圖片。
問題是我創建的第一個寵物等寵物後。該模板不會更新新的寵物。所以,它只顯示第一隻寵物。
所以當我刪除第一個寵物。儘管如此,它仍然顯示找不到頁面(404)。我給商店添加了新的寵物。
我試圖用的模板內循環,但它顯示的是我不能迭代它的錯誤,一個朋友告訴我,不要當你只呈現單個數據用於循環。
如何顯示在商店更多的寵物嗎?
我認爲這個問題是在我的animal.html也是我views.py
我animal.html
{% if pet %}
<li>Pet = {{ pet.animal }}</li>
<li>description = {{pet.description}} </li>
<img src="{{ pet.image.url }}">
{% endif %}
我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 %}
我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})
我莫dels.py
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.ForeignKey(Store)
image = models.FileField(upload_to="images/")
def __unicode__(self):
return self.animal
你會用我的situatio一個ManyToManyField或一個ForeignKey N + – donkeyboy72 2013-03-03 11:36:30
店鋪 – catherine 2013-03-03 11:41:52
擁有者的外鍵,當你點擊你想要顯示該店內寵物列表的商店時,對吧? – catherine 2013-03-03 11:42:23