1

ユーザーを転送するURLを作成するために使用しているフォームがあります。呼び出された私のフォームにはSearchForm、いくつかの BooleanFields、いくつかの ModelChoiceFields、いくつかの CharFields などがあります。フィールドは に設定されています) のようなrequired=False文字列に追加します。すべてのフィールドを反復処理し、次のようにする方法はありますか:search"FieldName=Value"

for item in form:
    name=item.name
    value=item.value

name にはフィールドの ID が入り、value にはユーザーが入力した値が入ります。

ありがとう!

4

2 に答える 2

1

あなたの仕事のために、フォームの使用cleaned_dataと方法を試す必要があります。def clean()

class SearchForm(forms.Form):
    # Everything as before.
    ...

    def clean(self):
        cleaned_data = super(ContactForm, self).clean()
        # get value of the field:
        # name_field = cleaned_data.get("name_field")

        if conditions:
            cleaned_data['FieldName'] = 'value'
        # If you will want add error messages 
        # when conditions are broken uncomment next line
        # raise forms.ValidationError("Errors!")

        # Always return the full collection of cleaned data.
        return cleaned_data

このアプローチは機能する必要があります。

于 2013-07-10T16:15:41.010 に答える