django-shop-simplevariations icon indicating copy to clipboard operation
django-shop-simplevariations copied to clipboard

Dynamic Form

Open AdrianRibao opened this issue 13 years ago • 0 comments

Hello, I've been playing a little bit with the code.

What I've done in this branch is to create a dinamic form called SimpleVariationsForm. The form takes the options and build a Django Form (only with option groups so far)

Then the form is used in the cart view.

The form can be extended to change the empty labels:

If we have 2 option groups with the slugs: 'color', 'size' then:

class ModifiedVariationsForm(SimpleVariationsForm):
    EMPTIES_LABELS = {
            'color': _(u'Color'),
            'size': _(u'Size'),
            }

If the form is invalid, the view redirects to other url. This url can be passed in the form:

<input type="hidden" name="next" value="{{ object.get_absolute_url }}"/>

if the form doesn't provide the next value, redirects to the cart url

The redirection URL adds a message that can be also specified in the form:

<input type="hidden" name="error_message" value="{% trans "Please, select size and color" %}"/>

I had corrected some bugs in lines 40 and 88 of shop_simplevariations/views.py

This is not intended to be merged, I just wanted to show you my work and listen to criticism. If you like it I can finish it with the option texts.

Is not finished since in my views I just extend the view ShopDetailWithVariationsView:

class MyShopDetailView(ShopDetailWithVariationsView):

I doesn't work if you don't create your own view that overrides the default ShopDetailView

As an example I have my own view like this:

myapp/urls.py:

url(r'^product/(?P<slug>[0-9A-Za-z-_.//]+)$', MyShopDetailView.as_view(), name='product_detail'),

myapp/views.py:

class MyShopDetailView(ShopDetailWithVariationsView):
    model = MyProduct
    form_class = VariationsForm

myapp/forms.py:

class VariationsForm(SimpleVariationsForm):
    EMPTIES_LABELS = {
            'color': _(u'Color'),
            'size': _(u'Size'),
            }

AdrianRibao avatar Sep 22 '11 12:09 AdrianRibao