Answer the question
In order to leave comments, you need to log in
How to properly implement asynchronous "AJAX" loading in django?
Good afternoon.
I am engaged in studying Djano and immediately implementing small ideas to consolidate knowledge.
So, at the stage of thinking about the implementation of AJAX Drag & Drop file uploads, a problem arose, how should it be correctly implemented from the server side?
For example, there is a certain table with different fields and containing models.FileField.
If we do asynchronous AJAX loading, as I understand it, we must save the file somewhere separately, and at the validation stage, simply transfer the file to "save it in the database". But then another question arises, how to save it correctly so as not to lose it and then correctly "pass it to the model".
In total, there are 3 implementations:
1. create a separate table for uploading files and bind it to the main model.
cons - data redundancy, it is necessary to implement tracking of garbage data.
2. write a separate AJAX handler for receiving data, which will save it to a temporary directory and send a link to the file through sessions, for example.
cons - I'm not sure that all this will do without big problems. you need to make sure that temporary folders are not littered
3. this is to change the logic of work, allow saving to the database without all the fields filled in, i.e. store the data incrementally into the model.
cons - incomprehensible logic of work - crutches.
For me now it seems to look towards 2 implementations . Search here for a better method.
I didn’t find answers on the Internet to this particular question, everywhere download examples go to send and save all the data at once, and not in parts.
I would like to hear an opinion or directions where it is worth sailing to resolve the issue.
Answer the question
In order to leave comments, you need to log in
In the view from request.FILES, you simply put it into the model and save
product = Product()
product.image = request.FILES['images']
product.save()
class ProductForm(models.ModelForm):
class Meta:
model = Product
def myview(request):
from = ProductForm(request.POST, request.FILES)
if form.is_valid():
product = form.save()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question