Cartridge form issue: TypeError creating Formset for AddProductForm

116 views
Skip to first unread message

Geoffrey Sechter

unread,
Jul 18, 2013, 5:07:08 PM7/18/13
to mezzani...@googlegroups.com
Hi all,

I'm trying to create a view where users can view and add all products from a custom model that inherits from Product to their cart. I feel like I've gotten close with a formset using Cartridge's stock AddProductForm but am hitting an "__init__() got multiple values for keyword argument 'data'" error on line 62 of Cartridge's forms.py. Here are some of the core relevant file sections:


### views.py ###

from mezzanine.core.views import direct_to_template

from .models import RetailProduct

from django.utils.functional import curry
from django.forms.formsets import formset_factory

from cartridge.shop.forms import AddProductForm

...

def product_formset(request):
    to_cart = (request.method == "POST" and
        request.POST.get("add_wishlist") is None)
    products = RetailProduct.objects.all()
    ProductFormSet = formset_factory(AddProductForm, extra = len(products))
    
    for product in products:
        ProductFormSet.form = staticmethod(curry(AddProductForm, request.POST or None, product=product, to_cart=to_cart))
    
    if request.method == 'POST':
        data = request.POST.copy()
        formset = ProductFormSet(request.POST)
        for form in formset:
            if form.is_valid():
                quantity = form.cleaned_data["quantity"]
                request.cart.add_item(form.variation, quantity)
    else:
        formset = ProductFormSet()
        
    return direct_to_template(request, 'product_formset.html', {'formset': formset})



### models.py ###

from cartridge.shop.models import Product

...

class RetailProduct(Product):
    tile_info = models.CharField(max_length = 64, null = True, blank = True)
    tile_image = models.ImageField(upload_to="product-images")
    nutritional_label = models.ImageField(upload_to="nutritional-labels")
    
    def __unicode__(self):
        return "Product: %s" % (self.title)



### product_formset.html ### (I will definitely spice this up once I get the formset serving correctly)

{% extends 'base.html' %}

{% load mezzanine_tags shop_tags %}

{% block content %}  

<form method="post" action="">{% csrf_token %}
    <table>
        {{ formset }}
        <input class="form-button pull-right" name="add_cart" type="submit" value="add to cart" />
    </table>
</form>

{% endblock %}



Any help or feedback would be greatly appreciated! 

Geoffrey Sechter

unread,
Jul 19, 2013, 8:04:29 PM7/19/13
to mezzani...@googlegroups.com
I got this resolved and believe I'm using Cartridge more as intended as a result. :) There was a change in requirements that simplified this as well - only one product needs to be added to the cart per submission.

Instead of building a formset, I created a product category page with all of my RetailProduct products. In my template for the product category page (using the base cartridge category template as a guide), I added a form within the product for-loop that had an action to cartridge's product view, passing in the slugified product to ensure the form is routed correctly:


{% for product in products.object_list %}
{% if product.retailproduct %}

...

<form class="product-form" action="{% url 'cartridge.shop.views.product' product|slugify %}" method="post">{% csrf_token %}
    <span>Qty:</span>
    <span class="form-product-quantity"><input id="id_quantity" name="quantity" type="text" value="1"></span>
    <span><input class="form-button" name="add_cart" type="submit" value="submit" /></span>
</form> 

...

{% endif %}
{% endfor %}


I'll try to tackle how to add multiple products for fun another time, when there aren't any deadline pressures.
Reply all
Reply to author
Forward
0 new messages