python - 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"),
]
解决方案
我想到了。我没有尝试将创建视图与提供 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)
推荐阅读
- python - 按月计算 np.npv
- c - 如何在 C 中使用 ffmpeg 捕获设备输入?
- amazon-web-services - 如何签署 CSR 以在 AWS ACM 中启用私有证书颁发机构 (CA)?(如何生成版本 3 证书?)
- c - 这个二维状态表是如何工作的
- blob - 使用 MyBatis 检索十六进制字符串
- python - 选择列表中包含的子字符串(没有语法字符)
- android - 如何将 DJI H264 FPV Feed 读取为 OpenCV Mat 对象?
- python - RecursionError:超出最大递归深度 - 简单程序
- java - Spring Boot:与两个数据库的事务,如果第二个数据库失败则回滚第一个数据库
- node.js - 是否可以将所有 url 路径重定向到 nodejs 应用程序中 App Engine 中的根处理程序?