从同一页面发布多个 django 表单、内联以及添加/删除这些内联

发布于 2024-08-04 07:33:34 字数 1326 浏览 3 评论 0原文

stackoverflow 中有几个关于或多或少相同问题的问题,但它们似乎都没有涵盖我可以预见的问题。由于我的 django 知识有限,我可能反应过度......所以......

我想用 django 完成的是在同一视图中编辑 2 个模型,List 和 ListItem。列表为通用表单,列表项为内联表单集。创建这两种形式不是问题。将它们传递到视图中>模板也不是问题。

我担心的是,

1)如何将它们邮寄到同一地址并处理该邮寄请求。

2) 如何使用 javascript 设置从该表单集中添加/删除那些内联 ListItems。

2.1)如果我向某个地址发送ajax请求以删除ListItem对象,然后使用javascript删除表单的该部分。 django 视图部分如何知道要从数据库中删除哪个对象?我是否需要将对象的 ID 传递给模板,以便我可以使用 ajax 将其发送回查看?

2.2)如果在我删除/添加行后发布整个更改的ListItem内联表单集,那么它不会引起问题,因为最初使用的对象字典在其间已被更改?

3)还有其他人能看到的陷阱吗?

顺便说一句,我不想​​通过代码示例。如果可以的话,请解释一下事情是如何运作的以及我应该做什么并记住。如果您知道最新的示例,那么我也可以使用一些链接。

编辑(并回答我自己的问题): 我尝试了一下,发现如下: 1)只需将它们放置...鉴于您可以这样做:

form = ListForm(request.POST, instance=l)
formset = ShoppingListFormSet(request.POST, instance=l)

然后做任何需要的事情 - 非常简单和容易。

2)复制现有的行/表格或删除一个。您需要记住的是,元素名称是正确的,并且表单集使用 {{ form.management_form }} 加载的内容包含最新且正确的信息以及表单的数量。所有字段名称也需要是最新的。如果从表格 1 和表格 3 之间删除表格,则表格 3 的数字需要更改为 2,依此类推。

2.1) 元素 id 可以从 {{ form.initial.id }} 中提取,然后在表单中使用

2.2) 如果 form.management_form 信息正确并且字段名称是最新的,则否(参见 2.)

3) 只需要构建视图,这样您的站点就不会被跨站点请求伪造破坏(请参阅 http://docs.djangoproject.com/en/dev/ref/contrib/csrf/

艾伦。

There are several questions about more or less same issue in stackoverflow, but none of them seems to cover the issues i can foresee. Since my django knowledege is limited i might be overreacting... so..

What i want to accomplish, with django, is to edit 2 models, List and ListItem in same view. List as common form and listitem as inlineformset. Creating those two forms is not a problem. Passing them into a view > template is not a problem either.

What i am worried about is,

1) How to post them to same address and handle that post request.

2) How to set up adding / removing those inline ListItems from this formset with javascript.

2.1) if i send ajax request to some address to remove ListItem object and then use javascript to remove that part of the form. How will the django view part know, which object to delete from the database? Do i need to pass ID of the object to template, so i can send it back to view with ajax?

2.2) If the whole changed ListItem inline formset gets posted after i have removed/added rows, then wont it cause problems, because the dictionary of objects that was used initially has been changed in between?

3)any other pitfalls someone could see?

Btw i dont want through code examples. If you can, just explain how things work and what i should do and keep in mind. If you know of up to date examples, then i could use some links too.

Edit (and answer to my own questions):
I tried it and here is what i found:
1) Just pos them... in view you can do it like this:

form = ListForm(request.POST, instance=l)
formset = ShoppingListFormSet(request.POST, instance=l)

And then do whatever is needed - very simple and easy.

2)Duplicate existing row/form or remove one. All you need to keep in mind is that element names are correct and the stuff that formset loads with {{ form.management_form }} contains up to date and correct information with how many forms there is. All field names also need to be up to date. If you remove form from between forms 1 and 3, then form 3 numbers need to be changed to 2 and so on.

2.1) element id can be extracted from {{ form.initial.id }} and then used in form

2.2) No if form.management_form info is correct and if field names are up to date (see 2.)

3) Just have to build views, so your site will not be ruined by cross site request forgery (see http://docs.djangoproject.com/en/dev/ref/contrib/csrf/)

Alan.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

始终不够爱げ你 2024-08-11 07:33:34

你所描述的做法很好。实际上,我刚刚发布了另一个 Django 问题的类似答案,您可以在这里看到 - Django 添加/删除表单,无需多次提交 当然,区别在于没有 javascript 来执行花哨的 AJAX 和客户端 HTML 修改。

上面提到的 REST 的基本要点(我知道过于简化)是一切都可以通过 URL 访问。 “Django 知道要删除哪条记录”的方式是,对于每个 URL,它都有一个映射到它的给定视图。视图接收记录 ID,然后知道要删除哪一条。

如果您还没有开始使用 AJAX,我建议您研究 Jquery,因为它提供了一些非常容易使用浏览器内置的 XML 请求对象的包装器。

顺便说一句,这只是一个建议,因为听起来您对此很陌生——首先让一切在没有ajax的情况下正常工作(即使执行操作会导致白屏或界面不佳),然后再添加ajax。当您不关心 AJAX 时,很容易进行调试,然后在知道一切都在服务器端运行后添加 AJAX。

The way you describe doing it is fine. I actually just posted a similar answer to another Django question which you can see here -- Django add / remove form without multiple submit The difference there of course being that there is not javascript to do the fancy AJAX and client-side HTML modification.

The basic gist of the REST as mentioned above (over simplified, i know) is that everything is accessible by a URL. The way "Django knows which record to delete" is that for each URL, it has a given view mapped to it. It's the view that receives the record ID and then knows which one to delete.

If you haven't already started toying with AJAX, I'd recommend looking into Jquery as it provides some very easy to use wrappers around the XML request objects built into the browsers.

By the way, just a recommendation since it sounds like you are new to this -- Get everything working WITHOUT ajax first (even if doing actions results in a white screen or poor interface), then add ajax second. It's easily to debug when you're not fussing with AJAX, and then to add AJAX after you know it all works on the server side.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文