Django URL 配置

发布于 2024-08-23 07:28:34 字数 552 浏览 6 评论 0原文

我有一个购买页面,它可以采用可选参数作为礼物,如果是礼物,则视图将礼物表单传递给模板,如果不是,则传递常规购买表单。

我的旧常规网址,它重定向到两个单独的视图:

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

视图是这样的:

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

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

这确实是一个糟糕的设计,因为两个视图几乎所有内容都相同,但所使用的表单不​​同。

我也考虑过使用 GET 并将礼物作为 GET 参数,但这不是一个好主意,因为我对这些页面使用 POST 方法,尤其是在验证后会导致问题。

我怎样才能使它成为一个单一的网址和一个单一的视图?

谢谢

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/

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

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

and the views was like this:


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

, 'gift_view'),

and the views was like this:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谈场末日恋爱 2024-08-30 07:28:34

urls.py

url(r'^(?P<item>[-\w]+)/purchase/

views.py

def purchase_view(request, item, gift=False):
    if gift:
        form = GiftForm
    else:
        form = PurchaseForm
    ...
, 'purchase_view', name='purchase_view'), url(r'^(?P<item>[-\w]+)/purchase/(?P<gift>gift)/

views.py


, 'purchase_view', name='gift_view'),

views.py

urls.py

url(r'^(?P<item>[-\w]+)/purchase/

views.py

def purchase_view(request, item, gift=False):
    if gift:
        form = GiftForm
    else:
        form = PurchaseForm
    ...
, 'purchase_view', name='purchase_view'), url(r'^(?P<item>[-\w]+)/purchase/(?P<gift>gift)/

views.py


, 'purchase_view', name='gift_view'),

views.py

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文