Django 3:UpdateView 的 get_object_or_404

我有一个问题:我想更新特定数据/产品,但无法使用get_object_or_404


views.py


from django.shortcuts import render,get_object_or_404

from django.http import HttpResponseRedirect, HttpResponse

from django.urls import reverse

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): 

    # 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.py


from django.urls import include, path

from 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 ?


请帮助。


跃然一笑
浏览 60回答 2
2回答

慕村225694

问题出在:productUpdate_form = ProductForm(instance=request.product1)不request包含product1属性,您只需传递product1对象即可:from django.shortcuts import render, get_object_or_404, redirectfrom django.http import HttpResponseRedirect, HttpResponsefrom django.contrib import messagesfrom django.views.generic import (&nbsp; &nbsp; UpdateView,&nbsp; &nbsp; DeleteView)from product.models import Productfrom pages.forms import ProductFormdef ProductUpdateView(request, pk):&nbsp;&nbsp; &nbsp; product1 = get_object_or_404(Product, pk=pk)&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; if request.method == 'POST':&nbsp; &nbsp; &nbsp; &nbsp; productUpdate_form = ProductForm(data=request.POST,files=request.FILES,instance=product1))&nbsp; &nbsp; &nbsp; &nbsp; # Check to see the form is valid&nbsp; &nbsp; &nbsp; &nbsp; if productUpdate_form.is_valid(): # and profile_default.is_valid() :&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # Sava o produto&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; productUpdate_form.save()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # Registration Successful! messages.success&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; messages.success(request, 'Produto Modificado com Sucesso')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; #Go to Index&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return redirect('index')&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # One of the forms was invalid if this else gets called.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(productUpdate_form.errors)&nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; # render the forms with data.&nbsp; &nbsp; &nbsp; &nbsp; productUpdate_form = ProductForm(instance=product1)&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; context = {'productUpdate_form': productUpdate_form,}&nbsp; &nbsp; return render(request, 'base/update.html',context)然而,这不是一个UpdateView:这不是一个基于类的视图,并且它不是从UpdateView.注意:函数通常用Snake_case编写,而不是PerlCase,因此建议将函数重命名为product_update_view, not ProductUpdateView。

吃鸡游戏

最好使用 ClassView# views.pyfrom django.views.generic.edit import UpdateViewfrom product.models import Productfrom django.contrib import messagesfrom pages.forms import ProductFormclass 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.pyfrom django.urls import include, pathfrom pages.views import (ProductListView,                        ProductUpdateView,                        ProductDeleteView)urlpatterns = [    path('listProduct/', ProductListView, name='listProduct'),    path('<int:pk>/update/', ProductUpdateView.as_view(), name='product-update'),]
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python