[英]Django 3: get_object_or_404 for UpdateView
我有一個問題:我想更新一個特定的數據/產品,但不能使用get_object_or_404
視圖.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)
網址.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, name='product-update'), <--this link is ok
]
錯誤:
/1/update/ 處的屬性錯誤
'WSGIRequest' 對象沒有屬性 'product1'
請求方法:GET 請求 URL: http : //127.0.0.1 : 8000/1/update/ Django 版本:3.1.1 異常類型:AttributeError 異常值:
'WSGIRequest' 對象沒有屬性 'product1'
異常位置:C:\\Users\\rodrigo negao\\Desktop\\PROJETOS\\MyDjango\\ECAPI\\pages\\views.py,第 78 行,在 ProductUpdateView Python 可執行文件中:C:\\Users\\rodrigo negao\\Anaconda3\\envs\\ECAPI\\python.exe Python 版本:3.8.5 Python 路徑:
['C:\\Users\\rodrigo negao\\Desktop\\PROJETOS\\MyDjango\\ECAPI', 'C:\\Users\\rodrigo negao\\Anaconda3\\envs\\ECAPI\\python38.zip', 'C:\\Users\\rodrigo negao\\Anaconda3\\ envs\\ECAPI\\DLLs'、'C:\\Users\\rodrigo negao\\Anaconda3\\envs\\ECAPI\\lib'、'C:\\Users\\rodrigo negao\\Anaconda3\\envs\\ECAPI'、'C:\\Users\\rodrigo negao\\ Anaconda3\\envs\\ECAPI\\lib\\site-packages']
服務器時間:2020 年 10 月 1 日星期四 21:36:44 -0300。
所以,我無法比較get_object_or_404 中的pk ,我需要它來找到並使用特定的數據/產品。
還有什么其他方法可以使用get_object_or_404或比較 link/pk 和 data/product ?
請幫忙。
問題位於:
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
,而不是。ProductUpdateView
最好使用 ClassView https://docs.djangoproject.com/en/3.1/ref/class-based-views/generic-editing/#django.views.generic.edit.UpdateView
# 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'),
]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.