1

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

1 Answer 1

3

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
    ...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.