ページに新しいフォームを追加するために ajax を使用する Web アプリケーションを作成しています。既存のフォームは inlineformset_factory を使用して作成されます。クライアント側がフォームを取得すると、それを正しい位置に挿入し、#id_form_type-TOTAL_FORMS" の値を更新します (この例では、form_type はフォームの名前です)。
既存のフォームには、name="form_type-1-source" のような各フィールドの名前があり、1 は一意のフォーム ID です。
問題は、すべてのフォームが異なるように、新しいフォームの次の ID を生成することです。私は1つの解決策を作りましたが、それは好きではありません。
ajax 呼び出しの URL は次のようになります。
(r'^ajax/get_form/(?P<form_type>\w+)$', views.get_form),
フォーム ビューを取得します。
def get_form(request, form_type):
"""
Returns form for requested model if invoken with ajax.
If invoken directly returns empty string.
The form is rendered into HTML using as_p method.
"""
if request.method == "GET" and request.is_ajax():
current_forms_no = int(request.GET["current_forms_no"])
form_class = all_forms[form_type]
Formset = inlineformset_factory(Component, form_class.Meta.model, extra=current_forms_no + 1, form = form_class, can_delete = False)
form = Formset()
return HttpResponse(form.forms[current_forms_no].as_p())
else:
return HttpResponse()
そして、それを呼び出す JavaScript 関数:
function add_new_form(event) {
event.preventDefault();
form_type = $(this).attr("href");
// for id generation
formset = $("#id_" + form_type + "-TOTAL_FORMS");
current_forms_no = formset.val()
container = $(this).parent().parent();
$.get("/ajax/get_form/" + form_type, {"current_forms_no" : current_forms_no}, function(data) {
container.append(data);
container.append("<hr>");
// increment form count so they can be saved on server
formset.val(parseInt(formset.val()) + 1);
})
}
私は複数のフォームを持っているので、関数は汎用的です。アンカーの href 属性に form_type を保持しています。
つまり、ここで行っているのは、必要以上のフォームを作成していて、最後のフォームだけを使用していることです。
では、最初に生成したい ID を inlineformset_factory に伝える方法はありますか?
PS: 下手な英語でごめんなさい...