views:

88

answers:

1

Hello, I have a purchase page, it can take an optional argument as a gift, if it is a gift, the view passes a gift form to the template and if not, a regular purchase form.

my old regular url, which redirects to two seperate views:

(r'^(?P<item>[-\w]+)/purchase/$', 'purchase_view'),
(r'^(?P<item>[-\w]+)/purchase/gift$', 'gift_view'),

and the views was like this:

def purchase_view(request,item):
....use purchase form

def gift_view(request,item):
....use giftform

It is a bad design indeed, as both views having are almost everything same but the forms used.

I have also thougt about using GET and giving gift as a GET param however it wasnt a good idea as I am using POST method for these pages, especially would cause issue after validation.

How can I make this a single url and a single view?

Thanks

+3  A: 

urls.py

url(r'^(?P<item>[-\w]+)/purchase/$', 'purchase_view', name='purchase_view'),
url(r'^(?P<item>[-\w]+)/purchase/(?P<gift>gift)/$', 'purchase_view', name='gift_view'),

views.py

def purchase_view(request, item, gift=False):
    if gift:
        form = GiftForm
    else:
        form = PurchaseForm
    ...
ironfroggy