python - python - 在Django中,有没有一种方法可以将对象信息传递给url和CreateView?

我尝试创建一个购物车元素,其中产品详细信息视图显示产品信息功能,

这是我的views.py


class ProductCreateView(CreateView):


template_name = '../templates/product/product_detail.html'


fields = [


 'title',


 'description',


 'category',


 'price',


 'summary',


 'image',


 'user',


 ]


model = Product



def dynamic_lookup_view(request, id):


 obj = get_object_or_404(Product, id=id)


 context = {


 'obj': obj,


 }


 return render(request,"product/product_detail.html", context)



这是我的urls.py


from django.conf import settings


from django.contrib import admin


from django.urls import path, include


from pages.views import home_view


from pages.views import contact_view


from pages.views import profile_view


from pages.views import our_staff_view


from pages.views import about_view


from pages.views import online_view


from pages.views import success_view


from products.views import product_detail_view


from products.views import dynamic_lookup_view


from pages.views import copyright_view


from django.conf.urls.static import static



urlpatterns = [


 path('', home_view, name='home'),


 path('contact/', contact_view, name='contact'),


 path('online/', online_view, name='online'),


 path('copyright/', copyright_view, name='copyright'),


 path('admin/', admin.site.urls),


 path('accounts/', include('accounts.urls')),


 path('accounts/', include('django.contrib.auth.urls')),


 path('profile/', profile_view, name='profile'),


 path('staff/', our_staff_view, name='our_staff'),


 path('about/', about_view, name="about"),


 path('product/', product_detail_view),


 path('product/<int:id>/', dynamic_lookup_view),


 path('success/', success_view, name="success"),


 ]



时间:

产品详细信息视图只是添加一个对象变量。


def dynamic_lookup_view(request, id):


if request.method == 'POST':


 form = ProductModelForm(request.POST, request.FILES or None)


 if form.is_valid():


 obj = Product() #gets new object


 obj.title = form.cleaned_data['title']


 obj.description = form.cleaned_data['description']


 obj.category = form.cleaned_data['category']


 obj.summary = form.cleaned_data['summary']


 obj.image = form.cleaned_data['image']


 obj.user = form.cleaned_data['user']


 form.save()


else:


 form = ProductModelForm()



obj = get_object_or_404(Product, id=id)


context = {


 'obj': obj,


 'form': form,


 }


return render(request,"product/product_detail.html", context)



...