开发者

Live editing in Django

开发者 https://www.devze.com 2022-12-16 00:15 出处:网络
Imagine you have an address book card. Normally the fields are displayed as static text, in a specific layout (imagine having multiple phone numbers, emails etc.). When editing it, you want to use the

Imagine you have an address book card. Normally the fields are displayed as static text, in a specific layout (imagine having multiple phone numbers, emails etc.). When editing it, you want to use the same layout, but with form fields instead of static text. It seems that the normal way of doing this in Django is to use separate views and templates, which forces you to duplicate all of the layout markup (ie, it's not DRY), and to change pages to sw开发者_Python百科itch between browsing and editing mode.

It would be nicer if you could switch in and out of editing mode on the fly, using JavaScript to replace the static text with form fields and vice versa, and Ajax to send changes to the server. I'm calling this "live editing", but perhaps there is a better term. At any rate, is there a recommended way of doing this in Django?

I was thinking of rendering, for each field, both a static and an editable version, and using JavaScript to hide and show them as needed. But I also need to update the static fields with new data from the server, and I need to take into account inline forms, and complex fields such as images (where the static display is an <img> tag, and you have to update the src after an upload). And I might also need to add and remove fields or fieldsets dynamically (again, consider inline formsets).

All in all, it'll take a lot of code. Is there an existing solution for Django, or a recommended approach? Otherwise, which JavaScript framework might be most helpful for this?


https://pypi.python.org/pypi/django-inplaceedit#information does exactly what you have asked for.


I haven't used it myself, but I've heard django-inline is designed to do this.


You may also want to have a look at the Javascript library jQuery and its inline editing capabilities.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号