質問

次のように見えるフォームがあります(簡潔にするために簡略化されています):

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_type 見える。今、a Pricing_type ラジオの選択、私はどちらも作ります pricing_flat_cost また pricing_percent_off 見える。これにより、より正確でユーザーフレンドリーなUIになります。

私の質問:値をどこから取るかを把握するロジックをコーディングするにはどうすればよいですか? Pricing_flat_pricepricing_percent_off 田畑?おそらく関数を作成する必要があります itemform それはそれを理解し、正しい値を返しますか?

それとも、誰かが提案できるよりクリーンなアプローチがあるのでしょうか?

役に立ちましたか?

解決

タイプ選択のカスタムウィジェットを定義し、JavaScriptコードを含めます 別の.jsファイルとして.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top