在 Django 表单中处理优雅降级

发布于 2024-09-27 08:12:39 字数 1467 浏览 1 评论 0原文

我有一个类似于以下内容的表单(为简洁起见进行了简化):

PRICING_PATTERN = r'(?:^\$?(?P<flat_price>\d+|\d?\.\d\d)$)|(?:^(?P<percent_off>\d+)\s*\%\s*off$)'
class ItemForm(forms.Form):
    pricing = forms.RegexField(
        label='Pricing',
        regex=PRICING_PATTERN
    )

    pricing_type = forms.CharField(
        label='Deal type',
        widget=forms.RadioSelect(
            choices=(
                ('flat_price','Flat price'),
                ('percent_off','Percentage off'),
            ),
           attrs={'style': 'display: none;'})
        ),
    )

    pricing_flat_price = forms.DecimalField(
        label='Flat price',
        max_digits=5,
        decimal_places=2,
        widget=forms.TextInput(attrs={'style': 'display: none;'})
    )

    pricing_percent_off = forms.IntegerField(
        label='Percent off',
        required=False,
        min_value=0,
        max_value=100,
        widget=forms.TextInput(attrs={'style': 'display: none;'})
    )

最初,为了优雅降级,只有定价可见。如果用户启用了 JavaScript,我会隐藏 pricing 并使 pricing_type 可见。现在,在 pricing_type 单选选项中,我将 pricing_flat_costpricing_percent_off 设为可见。这使得用户界面更加精确和用户友好。

我的问题:我应该如何编写逻辑来找出从 RegexField 或 pricing_flat_pricepricing_percent_off 字段中获取值的逻辑?我是否应该在 ItemForm 中创建一个函数来计算并返回正确的值?

或者也许有人可以建议一种更干净的方法?

I have a form that looks similar to the following (simplified for brevity):

PRICING_PATTERN = r'(?:^\$?(?P<flat_price>\d+|\d?\.\d\d)$)|(?:^(?P<percent_off>\d+)\s*\%\s*off$)'
class ItemForm(forms.Form):
    pricing = forms.RegexField(
        label='Pricing',
        regex=PRICING_PATTERN
    )

    pricing_type = forms.CharField(
        label='Deal type',
        widget=forms.RadioSelect(
            choices=(
                ('flat_price','Flat price'),
                ('percent_off','Percentage off'),
            ),
           attrs={'style': 'display: none;'})
        ),
    )

    pricing_flat_price = forms.DecimalField(
        label='Flat price',
        max_digits=5,
        decimal_places=2,
        widget=forms.TextInput(attrs={'style': 'display: none;'})
    )

    pricing_percent_off = forms.IntegerField(
        label='Percent off',
        required=False,
        min_value=0,
        max_value=100,
        widget=forms.TextInput(attrs={'style': 'display: none;'})
    )

Initially, for the purpose of graceful degradation, only pricing is visible. If the user has javascript enabled, I hide pricing and make pricing_type visible. Now, on a pricing_type radio selection, I make either pricing_flat_cost or pricing_percent_off visible. This makes for a more precise and user-friendly UI.

My questions: How should I go about coding the logic that figures out where to take the values from---the RegexField or the pricing_flat_price and pricing_percent_off fields? Should I perhaps create a function in ItemForm that figures it out and returns the correct value?

Or perhaps there's a cleaner approach that someone could suggest?

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

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

发布评论

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

评论(1

思念绕指尖 2024-10-04 08:12:39

为类型选择定义自定义小部件并包含 JavaScript 代码 作为单独的 .js 文件

Define a custom widget for the type selection and include the JavaScript code as a separate .js file.

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