我有一个问题:我想更新特定数据/产品,但无法使用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 ?
请帮助。
慕村225694
吃鸡游戏
相关分类