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

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

Django 3:UpdateView 的 get_object_or_404

Django 3:UpdateView 的 get_object_or_404

躍然一笑 2023-09-26 17:16:04
我有一個問題:我想更新特定數據/產品,但無法使用get_object_or_404views.pyfrom django.shortcuts import render,get_object_or_404from django.http import HttpResponseRedirect, HttpResponsefrom django.urls import reversefrom django.contrib import messagesfrom django.views.generic import (    UpdateView,    DeleteView)from product.models import Productfrom pages.forms import ProductFormdef ProductUpdateView(request, pk):     # queryset = Product.objects.all()[0].pk_id <-- I tried this    # queryset = Product.objects.get() <-- and this    queryset = Product.objects.all()    product1 = get_object_or_404(queryset, pk=pk)    #product1 = get_object_or_404(Product, pk=pk) <-- and this         if request.method == 'POST':        productUpdate_form = ProductForm(data=request.POST,files=request.FILES,instance=request.product1))        # Check to see the form is valid        if productUpdate_form.is_valid(): # and profile_default.is_valid() :            # Sava o produto            productUpdate_form.save()            # Registration Successful! messages.success            messages.success(request, 'Produto Modificado com Sucesso')            #Go to Index            return HttpResponseRedirect(reverse('index'))        else:            # One of the forms was invalid if this else gets called.            print(productUpdate_form.errors)    else:        # render the forms with data.        productUpdate_form = ProductForm(instance=request.product1)            context = {'productUpdate_form': productUpdate_form,}    return render(request, 'base/update.html',context)urls.pyfrom django.urls import include, pathfrom pages.views import (ProductListView,                        ProductUpdateView,                        ProductDeleteView)服務器時間:2020年10月1日星期四21:36:44 -0300。因此,我無法比較get_object_or_404中的pk,我需要它來找到并使用特定的數據/產品。還有什么其他方法可以使用get_object_or_404或比較 link/pk 和 data/product ?請幫助。
查看完整描述

2 回答

?
慕村225694

TA貢獻1880條經驗 獲得超4個贊

問題出在:


productUpdate_form = ProductForm(instance=request.product1)

不request包含product1屬性,您只需傳遞product1對象即可:


from django.shortcuts import render, get_object_or_404, redirect

from django.http import HttpResponseRedirect, HttpResponse

from django.contrib import messages

from django.views.generic import (

    UpdateView,

    DeleteView

)


from product.models import Product

from pages.forms import ProductForm


def ProductUpdateView(request, pk): 

    product1 = get_object_or_404(Product, pk=pk)

     

    if request.method == 'POST':

        productUpdate_form = ProductForm(data=request.POST,files=request.FILES,instance=product1))

        # Check to see the form is valid

        if productUpdate_form.is_valid(): # and profile_default.is_valid() :

            # Sava o produto

            productUpdate_form.save()

            # Registration Successful! messages.success

            messages.success(request, 'Produto Modificado com Sucesso')

            #Go to Index

            return redirect('index')

        else:

            # One of the forms was invalid if this else gets called.

            print(productUpdate_form.errors)


    else:

        # render the forms with data.

        productUpdate_form = ProductForm(instance=product1)

    

    

    context = {'productUpdate_form': productUpdate_form,}

    return render(request, 'base/update.html',context)

然而,這不是一個UpdateView:這不是一個基于類的視圖,并且它不是從UpdateView.


注意:函數通常用Snake_case編寫,而不是PerlCase,因此建議將函數重命名為product_update_view, not ProductUpdateView。


查看完整回答
反對 回復 2023-09-26
?
吃雞游戲

TA貢獻1829條經驗 獲得超7個贊

最好使用 ClassView

# views.py

from django.views.generic.edit import UpdateView

from product.models import Product

from django.contrib import messages

from pages.forms import ProductForm


class ProductUpdateView(UpdateView):

? ? model = Product

? ? form_class = ProductForm

? ? template_name = 'base/update.html'


? ? def form_valid(self, form):

? ? ? ? self.object = form.save()

? ? ? ? messages.success(self.request, 'Produto Modificado com Sucesso')

? ? ? ? return redirect('index')

?

? ? def get_context_data(self, **kwargs):

? ? ? ? if 'productUpdate_form' not in kwargs:

? ? ? ? ? ? kwargs['productUpdate_form'] = self.get_form()

? ? ? ? return super().get_context_data(**kwargs)

? ? ? ? ?


# urls.py

from django.urls import include, path

from pages.views import (ProductListView,

? ? ? ? ? ? ? ? ? ? ? ? ProductUpdateView,

? ? ? ? ? ? ? ? ? ? ? ? ProductDeleteView)


urlpatterns = [

? ? path('listProduct/', ProductListView, name='listProduct'),

? ? path('<int:pk>/update/', ProductUpdateView.as_view(), name='product-update'),

]


查看完整回答
反對 回復 2023-09-26
  • 2 回答
  • 0 關注
  • 125 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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