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

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

嘗試在我的 Django Web 應(yīng)用程序中添加“添加到愿望清單按鈕”

嘗試在我的 Django Web 應(yīng)用程序中添加“添加到愿望清單按鈕”

絕地?zé)o雙 2023-12-20 19:40:14
我正在嘗試在列出書籍的頁面上添加“添加到愿望清單”按鈕。當(dāng)用戶按下該按鈕時(shí),它會(huì)將 isbn(書籍的 pk)和用戶 ID 添加到愿望清單表中。由于某種原因,當(dāng)我按下按鈕時(shí),它沒有將它們添加到我的數(shù)據(jù)庫中。沒有顯示任何錯(cuò)誤,所以我不知道問題是什么。這是我的代碼#views.pyclass wishlistView(TemplateView):    template_name = 'TextSearch/wishlist.html'    def post(self, request, pk):        isbn = self.request.POST.get('isbn')        current_user = request.user        book = wishlist(userid_id = current_user.id, ISBN_id = isbn)        book.save()        return我的 HTML 代碼{% if user.is_authenticated %}    <form method="post">    {% csrf_token %}    <input type="hidden" value="{{book.pk}}" name="isbn">    <a href="{% url 'TextSearch:wishlist' book.pk %}" >Add To WishList</a>    </form>   {% else %}    <a href="{% url 'User:RegisterView'  %}"  >Add To WishList</a>   {% endif %}我的桌子#models.pyclass wishlist (models.Model):    userid = models.ForeignKey(User, on_delete= models.CASCADE)    ISBN = models.ForeignKey(books, on_delete = models.CASCADE)提前謝謝你,我是 Django 新手。
查看完整描述

2 回答

?
米脂

TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超3個(gè)贊

-- 問題已修復(fù) -- 這只是一個(gè) HTML 錯(cuò)誤,這是更新后的代碼


{% if user.is_authenticated %}

    <form action="{% url 'TextSearch:wishlist' book.pk %}" method="post">

    {% csrf_token %}

    <input type="hidden" value="{{book.pk}}" name="isbn">

    <input type="submit" value="Add to wishlist">

    </form>

   {% else %}

    <a href="{% url 'User:RegisterView'  %}"  >Add To WishList</a>


查看完整回答
反對(duì) 回復(fù) 2023-12-20
?
瀟湘沐

TA貢獻(xiàn)1816條經(jīng)驗(yàn) 獲得超6個(gè)贊

我個(gè)人使用這個(gè)方法


我的型號(hào):


class UserWishlist(models.Model):

user = models.ForeignKey(UserModel,on_delete=models.CASCADE,blank=False)

products = models.ManyToManyField(Product)

比我認(rèn)為的:


product = Product.objects.get(id=id)

obj, created = UserWishlist.objects.get_or_create(user=request.user)

if product in obj.products.all():

    obj.products.remove(product)

else:

    obj.products.add(product)

    return JsonResponse({'msg':'Item added to wishlist!'})

IMO 更容易,也有助于切換


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

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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