inlineformset_factory 自定义表单id
我正在编写一个 Web 应用程序,它使用 ajax 在页面上添加新表单。 现有表单是使用 inlineformset_factory 制作的。 当客户端获取表单时,它将其插入到正确的位置并更新 #id_form_type-TOTAL_FORMS 的值(form_type 是本例中表单的名称)。
现有表单的每个字段都有名称,例如 name="form_type-1- source”,其中 1 是唯一的表单 ID。
以便所有表单都不同。我已经制定了一个解决方案,但我不喜欢它。ajax
调用的 URL 如下所示:
(r'^ajax/get_form/(?P<form_type>\w+)$', views.get_form),
问题是为新表单生成下一个 ID , 表单视图:
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);
})
}
我有多个表单,因此函数是通用的。
因此,我在这里所做的是创建比我需要的更多表单,并且我只使用最后一个表单。 那么,
有什么方法可以告诉 inlineformset_factory 我想首先生成什么 ID?
PS:抱歉我的英语不好......
I am writing a web application that uses ajax to add new forms on the page. Existing forms are made using inlineformset_factory.
When client side gets the form it inserts it in the right position and updates the value of #id_form_type-TOTAL_FORMS" (form_type is name of the form in this example).
Existing forms have name for each field like name="form_type-1-source", where 1 is unique form ID.
The problem is generating next ID for new form so that all forms are different. I have made one solution, but I don't like it.
Url for the ajax call looks like this:
(r'^ajax/get_form/(?P<form_type>\w+)
Get form view:
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()
And JavaScript function that invokes that:
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);
})
}
I have more that one form, so functions are generic. I keep form_type in href attribute of anchor.
So, what I am doing here is creating more forms than I need and I only use last of them.
So, is there any way to tell inlineformset_factory what ID I want to be first to generate?
PS: Sorry for my bad English...
, views.get_form),
Get form view:
And JavaScript function that invokes that:
I have more that one form, so functions are generic. I keep form_type in href attribute of anchor.
So, what I am doing here is creating more forms than I need and I only use last of them.
So, is there any way to tell inlineformset_factory what ID I want to be first to generate?
PS: Sorry for my bad English...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我所做的是创建一个隐藏的额外表单,然后简单地使用 JavaScript 复制它。
即复制最后一个表单,使隐藏的表单可见,并隐藏表单的新副本。
这样您就可以计算页面上的表单数量并将其用于元 Total-Forms 变量。
如果您希望能够删除页面上的表单,则需要对 DELETED 属性进行一些处理。
编辑:这是我使用原型js的主要功能之一。
What I did was create an extra form that is hidden, and then simply copy it with Javascript.
That is copy the last form, make the hidden form Visible, and hide the new copy of the form.
This way you can just Count the number of forms on the page and use that for you meta Total-Forms variables.
Some fussing will need to be done with DELETED attributes if you want to be able to delete forms on the page.
Edit: Here is one of my main functions using prototype js.
您可以使用 auto_id 而不是生成表单集工厂参数 生成具有唯一 ID 的表单:
Instead of generating formset factory, you can use auto_id parameter to generate a form with unique ids: