我是 django 的新手,正在嘗試構建一個像網站這樣的 zomato。我想在點擊餐廳按鈕時填充餐廳菜單。我有保存所有餐廳數據的業務模型和保存餐廳菜單的菜單模型餐廳名稱作為外鍵,我已將餐廳填充到 home.html,如何在單擊特定餐廳后將特定餐廳的菜單填充到 store.html 希望我問的是正確的問題這是我的代碼models.pyclass Business(models.Model):bname=models.CharField(max_length=200,null=False)specialities=models.CharField(max_length=200,null=True)location=models.CharField(max_length=200,null=False)# image=models.ImageField(null=True,blank=True)def __str__(self): return self.bname@propertydef imageURL(self): try: url=self.image.url except: url= '' return urlclass Menu(models.Model): business=models.ForeignKey(Business,on_delete=models.CASCADE,blank=True,null=False) dish_name=models.CharField(max_length=200,null=False) price=models.IntegerField(null=False)def __str__(self): return self.dish_nameviews.pydef home(request):businesses= Business.objects.all()context={'businesses':businesses}return render(request,"foodapp/home.html",context)def store(request): menus=Menu.objects.get.all() context={'menus':menus} return render(request,"foodapp/store.html",context)主頁.html {% for business in businesses %} <div class="col-md-4"> <div class="box-element product"> <img class="thumbnail" src="{% static 'images/assets/placeholder.png' %}" alt=""> <hr> <h6><strong>{{business.bname}}</strong></h6> <hr> <h6>{{business.specialities}}</h6> <h6>{{business.location}} <button data-product={{business.id}} data-action="add" class="btn btn-outline-secondary add- btn update-cart"> <a href="{% url 'store' %}"> View</a></button> </h6> {% endfor %}
1 回答

小怪獸愛吃肉
TA貢獻1852條經驗 獲得超1個贊
您需要修改您的urls.py代碼以接受一個 id 以及storeex:store/1并在您的home.html更改網址中從<a href="{% url 'store' %}"> View</a></button>到<a href="{% url 'store' business.id %}"> View</a></button>
Urls.py
urlpatterns = [
# ...
path('store/<int:id>/', views.store),
# ...
]
Views.py
def store(request, id):
menus=Menu.objects.filter(business_id=id)
context={'menus':menus}
return render(request,"foodapp/store.html",context)
{% for menus in menu %}并修復store.html 中的錯誤:{% for menu in menus %}
添加回答
舉報
0/150
提交
取消