亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何從 onclick 獲取模型 ID

如何從 onclick 獲取模型 ID

千萬里不及你 2023-06-20 10:16:12
我是 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}} &nbsp;&nbsp;        <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 %}


查看完整回答
反對 回復 2023-06-20
  • 1 回答
  • 0 關注
  • 148 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號