第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何從 onclick 獲取模型 ID

如何從 onclick 獲取模型 ID

千萬(wàn)里不及你 2023-06-20 10:16:12
我是 django 的新手,正在嘗試構(gòu)建一個(gè)像網(wǎng)站這樣的 zomato。我想在點(diǎn)擊餐廳按鈕時(shí)填充餐廳菜單。我有保存所有餐廳數(shù)據(jù)的業(yè)務(wù)模型和保存餐廳菜單的菜單模型餐廳名稱作為外鍵,我已將餐廳填充到 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)主頁(yè).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貢獻(xiàn)1852條經(jīng)驗(yàn) 獲得超1個(gè)贊

您需要修改您的urls.py代碼以接受一個(gè) id 以及storeex:store/1并在您的home.html更改網(wǎng)址中從<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 %}并修復(fù)store.html 中的錯(cuò)誤:{% for menu in menus %}


查看完整回答
反對(duì) 回復(fù) 2023-06-20
  • 1 回答
  • 0 關(guān)注
  • 154 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)