找不到带有参数“('',)”的“ add_to_cart”。尝试了1种模式:

时间:2018-07-22 04:33:38

标签: python django views

我正在创建一个电子商务网站。我有一个产品详细信息页面,上面有一个按钮,它指向方法“ add_to_cart”。 “添加到购物车”需要URL中有一个item_id pk,才能将该商品添加到相应的order_item以便订购的对象中。

如何将item_id从当前产品详细信息页面发送到ProductDetailView中的add_to_cart视图

错误:

Exception Value:    
Reverse for 'add_to_cart' with arguments '('',)' not found. 1 pattern(s) tried: ['checkout/add-to-cart/(?P<item_id>[-\\w]+)/$']

portal / views.py

from django.shortcuts import render, redirect
from django.shortcuts import render
from django.views.generic import CreateView, DetailView, ListView, RedirectView, UpdateView
from django.views import generic

from django.contrib.auth.decorators import login_required  # This is to block pages to non users using function views
from django.contrib.auth.mixins import LoginRequiredMixin
from .forms import ProductForm, ContactForm
from .models import Product, Contact
from checkout.models import OrderItem, Order
from users.models import User, Profile

# Create your views here.

class ProductDetailView(generic.DetailView):
    model = Product
    template_name = 'portal/product_detail.html'

    def get_context_data(self, **kwargs):
            #item_id = self.kwargs['pk']
            filtered_orders = Order.objects.filter(owner=self.request.user.user_profile, is_ordered=False)
            current_order_products = []
            if filtered_orders.exists():
                user_order = filtered_orders[0]
                user_order_items = user_order.items.all()
                current_order_products = [product.product for product in user_order_items] #not sure what this does
            context = {
                'current_order_products': current_order_products
            }

            return context

portal / views.py

url(r'^product/(?P<pk>\d+)$', views.ProductDetailView.as_view(), name='product_detail'),

checkout / views.py

def add_to_cart(request, **kwargs):
    #get the user Profile
    user_profile = get_object_or_404(Profile, user=request.user)

    #filter products for id
    product = Product.objects.filter(id=kwargs.get('item_id', "")).first()

    #check if the user already owns the product
    if product in request.user.user_profile.merchandise.all():
        messages.info(request, "You already own this product")
        return redirect(reverse('product_list'))
    #create OrderItem of the selected product
    order_item, status = OrderItem.objects.get_or_create(product=product)
    #create order associate with the user
    user_order, status = Order.objects.get_or_create(owner=user_profile, is_ordered=False)
    user_order.items.add(order_item)
    if status:
        #generate a reference code
        user_order.ref_code = generate_order_id()
        user_order.save()

    #show confirmataion message and redirect to same page
    messages.info(request, "item added to cart")
    #return redirect(reverse('product_list'))
    return HttpResponseRedirect(request.META.get('HTTP_REFERER'))

checkout / urls.py

url(r'^add-to-cart/(?P<item_id>[-\w]+)/$', views.add_to_cart, name='add_to_cart'),

编辑:

它适用于我的ProductListView,而不是CBV

def ProductListView(request):
    model_list = Product.objects.filter(data_type="1")
    script_list = Product.objects.filter(data_type="2")
    filtered_orders = Order.objects.filter(owner=request.user.user_profile, is_ordered=False)
    current_order_products = []
    if filtered_orders.exists():
        user_order = filtered_orders[0]
        user_order_items = user_order.items.all()
        current_order_products = [product.product for product in user_order_items]

    context = {
        'model_list': model_list,
        'script_list': script_list,
        'current_order_products': current_order_products
    }

    return render(request, "portal/product_list.html", context)

0 个答案:

没有答案