首页 > 解决方案 > Is there a way to combine a function that passes object information to a url and CreateView in Django?

问题描述

I am trying to make a shopping cart element where a product detail view, that displays product information, has an add to cart feature. My thought process was to make a page that contains the information of a specific object but also allows for the page to be a CreateView that doesn't display a place to input information but has preset object information to create a clone of the product changing one variable to a certain User. That User can then go to a personal shopping cart where it just lists all the products under his/her Username.

The problem is, I can't combine these two things together and it's clearly based on a lack of ability (It's either the dynamic_lookup_view gives information or the CreateView gives a form, not both). I've tried using get_context_data and forms of mixins, even placing the function in the class. Some guidance or a pearl to the right track would be greatly appreciated.

Here is my 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)

Here is my 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"),
 ]

标签: pythondjangodjango-formsdjango-views

解决方案


我想到了。我没有尝试将创建视图与提供 obj 信息的函数结合起来,而是将这两个函数组合在一起。我制作了一个将模型形式放入网站的功能。使用表单中的这些信息,我只需创建一个新对象 inviews.py。而产品详细视图只是添加了一个对象变量。

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)

推荐阅读