On Tue, Jan 8, 2013 at 6:03 AM, Randa Hisham <randahes...@gmail.com> wrote:
> > i have list of images > i send it and display as table in django template > i make user to change add delete images > how to save back this changes > -- > Randa Hesham > Software Developer > > When you say you allow the user to add images, where do the images come from? If these are images already available on your server, which the user simply selects, using JavaScript that you have written, then have only to send the rearranged list back to the server. This can be done with a POST, either using AJAX, or a more traditional FORM tag POST that also reloads the page. The latter is the simpler approach to make functional, at the cost of letting the user see the page reload, but once it is working, it serves as a head start on the AJAX approach. In either case your first task is to decide how the image selection information is to be stored in the server's database. A flexible approach is to have a "Picture" model which mostly contains an ImageField, allowing for image upload withing Django, among other things. Then the model of the pages containing the images is related to the Picture model via a ManyToMany relation, though one "through" an intermediate model which has an order integer field, or perhaps row and column integer fields. This may be a more complex structure than you require, but should suffice for most needs. Next, the JavaScript that re-orders/selects/deselects the images must also update a field or fields in a form tag so that when the form's submit button is clicked, the new arrangement is transmitted to the server. (You could, alternatively, catch the submit and update the form field at that time.) Because the number of images, and, presumably, the number of rows, is variable, I suggest a single textarea field in which you store JSON for the arrangement (e.g.; arrays of arrays of integers, where the integers are the database IDs of the Picture models). Again, other arrangements are possible. One might be to send information about what changed, rather than the entire arrangement of images, but this is more brittle. Finally, the view which receives the POST is responsible for decoding the JSON and updating the intermediate model instances to correspond to the new arrangement. One approach is to delete all the intermediate model instances (relations) and recreate new ones from the submitted data, and I suggest that you start with this. It may (or may not) save database overhead to figure out what changed and just update or replace relations that have changed. The AJAX approach will also be doing a POST, using, for example, jQuery's post method, and the view will be nearly the same (it need not return the new HTML version of the page in this case). The AJAX POSTs would happen if response to a user change to the page, rather than requiring a submit button be pressed, and are much more likely to benefit from transmitting only what has changed, rather than the entire arrangement, but this can be left as an eventual optimization. Bill -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.