Djangoフレームワークのフォームフィールドから値を取得するにはどうすればいいですか? 質問する

Djangoフレームワークのフォームフィールドから値を取得するにはどうすればいいですか? 質問する

Django フレームワークのフォーム フィールドから値を取得するにはどうすればよいですか? テンプレートではなくビューでこれを実行したいのですが...

ベストアンサー1

ビューでフォームを使用するかなり説明がつきます。

ビューでフォームを処理するための標準的なパターンは次のようになります。

def contact(request):
    if request.method == 'POST': # If the form has been submitted...
        form = ContactForm(request.POST) # A form bound to the POST data
        if form.is_valid(): # All validation rules pass
            # Process the data in form.cleaned_data
            # ...

            print form.cleaned_data['my_form_field_name']

            return HttpResponseRedirect('/thanks/') # Redirect after POST
    else:
        form = ContactForm() # An unbound form

    return render_to_response('contact.html', {
        'form': form,
    })

おすすめ記事