How to send django multiple filefield's form data through ajax
I've a django form for FileField
where I can input multiple files. I'm trying to send this list of files from django template to views using ajax.
But in my views I'm getting False
for form.is_valid
. Also, form.errors
shows:
file_form.errors <ul class="errorlist"><li>file<ul class="errorlist"><li>This fi
eld is required.</li></ul></li></ul>
I'm sure I'm not passing the data correctly through ajax. Do you have any idea where I'm going wrong?
My template:
<form id="file-form" enctype="multipart/form-data", method="POST">
{% csrf_token %}
<div id="id_file">{{ file_form }}</div>
<br><br>
<input type="submit" value="Upload files" name="_upload_file"/>
</form>
<script type="text/javascript">
$(document).on('submit', '#file-form', function(e)){
e.preventDefault();
$.ajax({
type:'POST',
url:'/create-post/',
data:{
files:$('#id_file').val(),
csrfmiddlewaretoken:$('input[name=csrfmiddlewaretoken]').val()
},
success:function(){
}
})
}
</script>
views.py
def upload_files(request): # create_to_feed
if request.method == 'POST':
if PostMediaModelForm(request.POST, request.FILES):
file_form = PostMediaModelForm(request.POST, request.FILES)
print("request.post", request.POST)
''' request.post <QueryDict: {'csrfmiddlewaretoken': ['CKsF77Sirdfgb72QG5oic86Wc0rHt
EIW7WYjw78tB9lDwOz5wwh1msdOymgGi9mh', 'CKsF77Sirdfgb72QG5oic86Wc0rHtEIW7WYjw78tB
9lDwOz5wwh1msdOymgGi9mh'], 'post_body': ['tests going on'], 'file': ['1.jpg', '2
.jpg', '3.jpg'], '_upload_file': ['Upload files']}> '''
print("request.FILES", request.FILES) # request.FILES <MultiValueDict: {}>
print("file_form.errors", file_form.errors) # shows the errors mentioned above
print("file form", file_form.data)
''' file form <QueryDict: {'csrfmiddlewaretoken': ['CKsF77Sirdfgb72QG5oic86Wc0rHtEIW
7WYjw78tB9lDwOz5wwh1msdOymgGi9mh', 'CKsF77Sirdfgb72QG5oic86Wc0rHtEIW7WYjw78tB9lD
wOz5wwh1msdOymgGi9mh'], 'post_body': ['tests going on'], 'file': ['1.jpg', '2.jp
g', '3.jpg'], '_upload_file': ['Upload files']}> '''
print("files are: ", file_form.data.getlist('file')) # files are: ['1.jpg', '2.jpg', '3.jpg']
print("file_form.is_valid()", file_form.is_valid(), "'_upload_file' in request.POST.data", '_upload_file' in request.POST)
# file_form.is_valid() False '_upload_file' in request.POST.data True
if '_upload_file' in request.POST:
files = request.FILES.getlist('file')
print("files: ", files) # files:
print("no. of files: ", len(files)) # no. of files: 0
for f in files:
PostMedia.file = f
PostMedia.save(update_fields=["file"])
messages.success(request, 'File(s) uploaded!')
# return redirect('submit_post')
return HttpResponse('')
# else:
# return HttpResponse(str(file_form.errors))
elif PostModelForm(request.POST):
form = PostModelForm(request.POST)
if form.is_valid and '_create_post' in request.POST:
feed_instance = form.save(commit=False)
PostMedia.body = feed_instance
feed_instance.save()
PostMedia.save(update_fields=["body"])
messages.success(request, 'Your post was submitted successfully !')
return redirect('/create-post')
else:
form = PostModelForm()
file_form = PostMediaModelForm()
return render(request, 'multiuploads/form.html', {'form': form, 'file_form': file_form})
models.py
class Post(models.Model):
post_body = models.TextField(blank=False, max_length=500)
def __str__(self):
return str(self.post_body)
class PostMedia(models.Model):
file = models.FileField(blank=False, null=False, validators=[FileExtensionValidator(['jpg'])])
body = models.ForeignKey(Post, on_delete=models.CASCADE)
forms.py
class PostModelForm(ModelForm):
class Meta:
model = Post
fields = ['post_body']
class PostMediaModelForm(ModelForm):
class Meta:
model = PostMedia
fields = ['file']
widgets = {
'file': ClearableFileInput(attrs={'multiple': True}),
}
This is the my front end:
As you can see after clicking the upload button I should be able to save the form as in upload the files in the background while still staying on the same page and the once the I see file uploaded message I can be able to submit the whole form.
Thanks in advance!
jquery ajax django django-templates django-views
|
show 9 more comments
I've a django form for FileField
where I can input multiple files. I'm trying to send this list of files from django template to views using ajax.
But in my views I'm getting False
for form.is_valid
. Also, form.errors
shows:
file_form.errors <ul class="errorlist"><li>file<ul class="errorlist"><li>This fi
eld is required.</li></ul></li></ul>
I'm sure I'm not passing the data correctly through ajax. Do you have any idea where I'm going wrong?
My template:
<form id="file-form" enctype="multipart/form-data", method="POST">
{% csrf_token %}
<div id="id_file">{{ file_form }}</div>
<br><br>
<input type="submit" value="Upload files" name="_upload_file"/>
</form>
<script type="text/javascript">
$(document).on('submit', '#file-form', function(e)){
e.preventDefault();
$.ajax({
type:'POST',
url:'/create-post/',
data:{
files:$('#id_file').val(),
csrfmiddlewaretoken:$('input[name=csrfmiddlewaretoken]').val()
},
success:function(){
}
})
}
</script>
views.py
def upload_files(request): # create_to_feed
if request.method == 'POST':
if PostMediaModelForm(request.POST, request.FILES):
file_form = PostMediaModelForm(request.POST, request.FILES)
print("request.post", request.POST)
''' request.post <QueryDict: {'csrfmiddlewaretoken': ['CKsF77Sirdfgb72QG5oic86Wc0rHt
EIW7WYjw78tB9lDwOz5wwh1msdOymgGi9mh', 'CKsF77Sirdfgb72QG5oic86Wc0rHtEIW7WYjw78tB
9lDwOz5wwh1msdOymgGi9mh'], 'post_body': ['tests going on'], 'file': ['1.jpg', '2
.jpg', '3.jpg'], '_upload_file': ['Upload files']}> '''
print("request.FILES", request.FILES) # request.FILES <MultiValueDict: {}>
print("file_form.errors", file_form.errors) # shows the errors mentioned above
print("file form", file_form.data)
''' file form <QueryDict: {'csrfmiddlewaretoken': ['CKsF77Sirdfgb72QG5oic86Wc0rHtEIW
7WYjw78tB9lDwOz5wwh1msdOymgGi9mh', 'CKsF77Sirdfgb72QG5oic86Wc0rHtEIW7WYjw78tB9lD
wOz5wwh1msdOymgGi9mh'], 'post_body': ['tests going on'], 'file': ['1.jpg', '2.jp
g', '3.jpg'], '_upload_file': ['Upload files']}> '''
print("files are: ", file_form.data.getlist('file')) # files are: ['1.jpg', '2.jpg', '3.jpg']
print("file_form.is_valid()", file_form.is_valid(), "'_upload_file' in request.POST.data", '_upload_file' in request.POST)
# file_form.is_valid() False '_upload_file' in request.POST.data True
if '_upload_file' in request.POST:
files = request.FILES.getlist('file')
print("files: ", files) # files:
print("no. of files: ", len(files)) # no. of files: 0
for f in files:
PostMedia.file = f
PostMedia.save(update_fields=["file"])
messages.success(request, 'File(s) uploaded!')
# return redirect('submit_post')
return HttpResponse('')
# else:
# return HttpResponse(str(file_form.errors))
elif PostModelForm(request.POST):
form = PostModelForm(request.POST)
if form.is_valid and '_create_post' in request.POST:
feed_instance = form.save(commit=False)
PostMedia.body = feed_instance
feed_instance.save()
PostMedia.save(update_fields=["body"])
messages.success(request, 'Your post was submitted successfully !')
return redirect('/create-post')
else:
form = PostModelForm()
file_form = PostMediaModelForm()
return render(request, 'multiuploads/form.html', {'form': form, 'file_form': file_form})
models.py
class Post(models.Model):
post_body = models.TextField(blank=False, max_length=500)
def __str__(self):
return str(self.post_body)
class PostMedia(models.Model):
file = models.FileField(blank=False, null=False, validators=[FileExtensionValidator(['jpg'])])
body = models.ForeignKey(Post, on_delete=models.CASCADE)
forms.py
class PostModelForm(ModelForm):
class Meta:
model = Post
fields = ['post_body']
class PostMediaModelForm(ModelForm):
class Meta:
model = PostMedia
fields = ['file']
widgets = {
'file': ClearableFileInput(attrs={'multiple': True}),
}
This is the my front end:
As you can see after clicking the upload button I should be able to save the form as in upload the files in the background while still staying on the same page and the once the I see file uploaded message I can be able to submit the whole form.
Thanks in advance!
jquery ajax django django-templates django-views
2
Have you checked the answers in: stackoverflow.com/questions/2320069/jquery-ajax-file-upload ?
– schillingt
Jan 18 at 19:46
@schillingt I don't want to upload file through ajax, I want to POST the list of input files and file uploading will be done in the django views. I tried to pass them in the data attribute of ajax.I've attached the code, will you please check out and tell me the correct code?
– user10058776
Jan 19 at 7:26
@ThomasJiang can you answer this?
– user10058776
Jan 19 at 9:50
You're making an ajax call to submit the form but you don't want to upload the file through ajax? Can you elaborate what you're trying to do? Your form is expecting to receive files (including the file data) so what do you mean by "file uploading will be done in the django view?" And maybe show us yourCreatePost
view to see how you handle the ajax post.
– dirkgroten
Jan 21 at 10:04
@dirkgroten I've given everything now in the edited question. I don't know anything about ajax, but this task required me to use it. I believe I've to send(post) the form data through ajax to django views where my form will be saved(files will be saved)
– user10058776
Jan 22 at 7:31
|
show 9 more comments
I've a django form for FileField
where I can input multiple files. I'm trying to send this list of files from django template to views using ajax.
But in my views I'm getting False
for form.is_valid
. Also, form.errors
shows:
file_form.errors <ul class="errorlist"><li>file<ul class="errorlist"><li>This fi
eld is required.</li></ul></li></ul>
I'm sure I'm not passing the data correctly through ajax. Do you have any idea where I'm going wrong?
My template:
<form id="file-form" enctype="multipart/form-data", method="POST">
{% csrf_token %}
<div id="id_file">{{ file_form }}</div>
<br><br>
<input type="submit" value="Upload files" name="_upload_file"/>
</form>
<script type="text/javascript">
$(document).on('submit', '#file-form', function(e)){
e.preventDefault();
$.ajax({
type:'POST',
url:'/create-post/',
data:{
files:$('#id_file').val(),
csrfmiddlewaretoken:$('input[name=csrfmiddlewaretoken]').val()
},
success:function(){
}
})
}
</script>
views.py
def upload_files(request): # create_to_feed
if request.method == 'POST':
if PostMediaModelForm(request.POST, request.FILES):
file_form = PostMediaModelForm(request.POST, request.FILES)
print("request.post", request.POST)
''' request.post <QueryDict: {'csrfmiddlewaretoken': ['CKsF77Sirdfgb72QG5oic86Wc0rHt
EIW7WYjw78tB9lDwOz5wwh1msdOymgGi9mh', 'CKsF77Sirdfgb72QG5oic86Wc0rHtEIW7WYjw78tB
9lDwOz5wwh1msdOymgGi9mh'], 'post_body': ['tests going on'], 'file': ['1.jpg', '2
.jpg', '3.jpg'], '_upload_file': ['Upload files']}> '''
print("request.FILES", request.FILES) # request.FILES <MultiValueDict: {}>
print("file_form.errors", file_form.errors) # shows the errors mentioned above
print("file form", file_form.data)
''' file form <QueryDict: {'csrfmiddlewaretoken': ['CKsF77Sirdfgb72QG5oic86Wc0rHtEIW
7WYjw78tB9lDwOz5wwh1msdOymgGi9mh', 'CKsF77Sirdfgb72QG5oic86Wc0rHtEIW7WYjw78tB9lD
wOz5wwh1msdOymgGi9mh'], 'post_body': ['tests going on'], 'file': ['1.jpg', '2.jp
g', '3.jpg'], '_upload_file': ['Upload files']}> '''
print("files are: ", file_form.data.getlist('file')) # files are: ['1.jpg', '2.jpg', '3.jpg']
print("file_form.is_valid()", file_form.is_valid(), "'_upload_file' in request.POST.data", '_upload_file' in request.POST)
# file_form.is_valid() False '_upload_file' in request.POST.data True
if '_upload_file' in request.POST:
files = request.FILES.getlist('file')
print("files: ", files) # files:
print("no. of files: ", len(files)) # no. of files: 0
for f in files:
PostMedia.file = f
PostMedia.save(update_fields=["file"])
messages.success(request, 'File(s) uploaded!')
# return redirect('submit_post')
return HttpResponse('')
# else:
# return HttpResponse(str(file_form.errors))
elif PostModelForm(request.POST):
form = PostModelForm(request.POST)
if form.is_valid and '_create_post' in request.POST:
feed_instance = form.save(commit=False)
PostMedia.body = feed_instance
feed_instance.save()
PostMedia.save(update_fields=["body"])
messages.success(request, 'Your post was submitted successfully !')
return redirect('/create-post')
else:
form = PostModelForm()
file_form = PostMediaModelForm()
return render(request, 'multiuploads/form.html', {'form': form, 'file_form': file_form})
models.py
class Post(models.Model):
post_body = models.TextField(blank=False, max_length=500)
def __str__(self):
return str(self.post_body)
class PostMedia(models.Model):
file = models.FileField(blank=False, null=False, validators=[FileExtensionValidator(['jpg'])])
body = models.ForeignKey(Post, on_delete=models.CASCADE)
forms.py
class PostModelForm(ModelForm):
class Meta:
model = Post
fields = ['post_body']
class PostMediaModelForm(ModelForm):
class Meta:
model = PostMedia
fields = ['file']
widgets = {
'file': ClearableFileInput(attrs={'multiple': True}),
}
This is the my front end:
As you can see after clicking the upload button I should be able to save the form as in upload the files in the background while still staying on the same page and the once the I see file uploaded message I can be able to submit the whole form.
Thanks in advance!
jquery ajax django django-templates django-views
I've a django form for FileField
where I can input multiple files. I'm trying to send this list of files from django template to views using ajax.
But in my views I'm getting False
for form.is_valid
. Also, form.errors
shows:
file_form.errors <ul class="errorlist"><li>file<ul class="errorlist"><li>This fi
eld is required.</li></ul></li></ul>
I'm sure I'm not passing the data correctly through ajax. Do you have any idea where I'm going wrong?
My template:
<form id="file-form" enctype="multipart/form-data", method="POST">
{% csrf_token %}
<div id="id_file">{{ file_form }}</div>
<br><br>
<input type="submit" value="Upload files" name="_upload_file"/>
</form>
<script type="text/javascript">
$(document).on('submit', '#file-form', function(e)){
e.preventDefault();
$.ajax({
type:'POST',
url:'/create-post/',
data:{
files:$('#id_file').val(),
csrfmiddlewaretoken:$('input[name=csrfmiddlewaretoken]').val()
},
success:function(){
}
})
}
</script>
views.py
def upload_files(request): # create_to_feed
if request.method == 'POST':
if PostMediaModelForm(request.POST, request.FILES):
file_form = PostMediaModelForm(request.POST, request.FILES)
print("request.post", request.POST)
''' request.post <QueryDict: {'csrfmiddlewaretoken': ['CKsF77Sirdfgb72QG5oic86Wc0rHt
EIW7WYjw78tB9lDwOz5wwh1msdOymgGi9mh', 'CKsF77Sirdfgb72QG5oic86Wc0rHtEIW7WYjw78tB
9lDwOz5wwh1msdOymgGi9mh'], 'post_body': ['tests going on'], 'file': ['1.jpg', '2
.jpg', '3.jpg'], '_upload_file': ['Upload files']}> '''
print("request.FILES", request.FILES) # request.FILES <MultiValueDict: {}>
print("file_form.errors", file_form.errors) # shows the errors mentioned above
print("file form", file_form.data)
''' file form <QueryDict: {'csrfmiddlewaretoken': ['CKsF77Sirdfgb72QG5oic86Wc0rHtEIW
7WYjw78tB9lDwOz5wwh1msdOymgGi9mh', 'CKsF77Sirdfgb72QG5oic86Wc0rHtEIW7WYjw78tB9lD
wOz5wwh1msdOymgGi9mh'], 'post_body': ['tests going on'], 'file': ['1.jpg', '2.jp
g', '3.jpg'], '_upload_file': ['Upload files']}> '''
print("files are: ", file_form.data.getlist('file')) # files are: ['1.jpg', '2.jpg', '3.jpg']
print("file_form.is_valid()", file_form.is_valid(), "'_upload_file' in request.POST.data", '_upload_file' in request.POST)
# file_form.is_valid() False '_upload_file' in request.POST.data True
if '_upload_file' in request.POST:
files = request.FILES.getlist('file')
print("files: ", files) # files:
print("no. of files: ", len(files)) # no. of files: 0
for f in files:
PostMedia.file = f
PostMedia.save(update_fields=["file"])
messages.success(request, 'File(s) uploaded!')
# return redirect('submit_post')
return HttpResponse('')
# else:
# return HttpResponse(str(file_form.errors))
elif PostModelForm(request.POST):
form = PostModelForm(request.POST)
if form.is_valid and '_create_post' in request.POST:
feed_instance = form.save(commit=False)
PostMedia.body = feed_instance
feed_instance.save()
PostMedia.save(update_fields=["body"])
messages.success(request, 'Your post was submitted successfully !')
return redirect('/create-post')
else:
form = PostModelForm()
file_form = PostMediaModelForm()
return render(request, 'multiuploads/form.html', {'form': form, 'file_form': file_form})
models.py
class Post(models.Model):
post_body = models.TextField(blank=False, max_length=500)
def __str__(self):
return str(self.post_body)
class PostMedia(models.Model):
file = models.FileField(blank=False, null=False, validators=[FileExtensionValidator(['jpg'])])
body = models.ForeignKey(Post, on_delete=models.CASCADE)
forms.py
class PostModelForm(ModelForm):
class Meta:
model = Post
fields = ['post_body']
class PostMediaModelForm(ModelForm):
class Meta:
model = PostMedia
fields = ['file']
widgets = {
'file': ClearableFileInput(attrs={'multiple': True}),
}
This is the my front end:
As you can see after clicking the upload button I should be able to save the form as in upload the files in the background while still staying on the same page and the once the I see file uploaded message I can be able to submit the whole form.
Thanks in advance!
jquery ajax django django-templates django-views
jquery ajax django django-templates django-views
edited Jan 22 at 7:36
user10058776
asked Jan 18 at 19:01
user10058776user10058776
348
348
2
Have you checked the answers in: stackoverflow.com/questions/2320069/jquery-ajax-file-upload ?
– schillingt
Jan 18 at 19:46
@schillingt I don't want to upload file through ajax, I want to POST the list of input files and file uploading will be done in the django views. I tried to pass them in the data attribute of ajax.I've attached the code, will you please check out and tell me the correct code?
– user10058776
Jan 19 at 7:26
@ThomasJiang can you answer this?
– user10058776
Jan 19 at 9:50
You're making an ajax call to submit the form but you don't want to upload the file through ajax? Can you elaborate what you're trying to do? Your form is expecting to receive files (including the file data) so what do you mean by "file uploading will be done in the django view?" And maybe show us yourCreatePost
view to see how you handle the ajax post.
– dirkgroten
Jan 21 at 10:04
@dirkgroten I've given everything now in the edited question. I don't know anything about ajax, but this task required me to use it. I believe I've to send(post) the form data through ajax to django views where my form will be saved(files will be saved)
– user10058776
Jan 22 at 7:31
|
show 9 more comments
2
Have you checked the answers in: stackoverflow.com/questions/2320069/jquery-ajax-file-upload ?
– schillingt
Jan 18 at 19:46
@schillingt I don't want to upload file through ajax, I want to POST the list of input files and file uploading will be done in the django views. I tried to pass them in the data attribute of ajax.I've attached the code, will you please check out and tell me the correct code?
– user10058776
Jan 19 at 7:26
@ThomasJiang can you answer this?
– user10058776
Jan 19 at 9:50
You're making an ajax call to submit the form but you don't want to upload the file through ajax? Can you elaborate what you're trying to do? Your form is expecting to receive files (including the file data) so what do you mean by "file uploading will be done in the django view?" And maybe show us yourCreatePost
view to see how you handle the ajax post.
– dirkgroten
Jan 21 at 10:04
@dirkgroten I've given everything now in the edited question. I don't know anything about ajax, but this task required me to use it. I believe I've to send(post) the form data through ajax to django views where my form will be saved(files will be saved)
– user10058776
Jan 22 at 7:31
2
2
Have you checked the answers in: stackoverflow.com/questions/2320069/jquery-ajax-file-upload ?
– schillingt
Jan 18 at 19:46
Have you checked the answers in: stackoverflow.com/questions/2320069/jquery-ajax-file-upload ?
– schillingt
Jan 18 at 19:46
@schillingt I don't want to upload file through ajax, I want to POST the list of input files and file uploading will be done in the django views. I tried to pass them in the data attribute of ajax.I've attached the code, will you please check out and tell me the correct code?
– user10058776
Jan 19 at 7:26
@schillingt I don't want to upload file through ajax, I want to POST the list of input files and file uploading will be done in the django views. I tried to pass them in the data attribute of ajax.I've attached the code, will you please check out and tell me the correct code?
– user10058776
Jan 19 at 7:26
@ThomasJiang can you answer this?
– user10058776
Jan 19 at 9:50
@ThomasJiang can you answer this?
– user10058776
Jan 19 at 9:50
You're making an ajax call to submit the form but you don't want to upload the file through ajax? Can you elaborate what you're trying to do? Your form is expecting to receive files (including the file data) so what do you mean by "file uploading will be done in the django view?" And maybe show us your
CreatePost
view to see how you handle the ajax post.– dirkgroten
Jan 21 at 10:04
You're making an ajax call to submit the form but you don't want to upload the file through ajax? Can you elaborate what you're trying to do? Your form is expecting to receive files (including the file data) so what do you mean by "file uploading will be done in the django view?" And maybe show us your
CreatePost
view to see how you handle the ajax post.– dirkgroten
Jan 21 at 10:04
@dirkgroten I've given everything now in the edited question. I don't know anything about ajax, but this task required me to use it. I believe I've to send(post) the form data through ajax to django views where my form will be saved(files will be saved)
– user10058776
Jan 22 at 7:31
@dirkgroten I've given everything now in the edited question. I don't know anything about ajax, but this task required me to use it. I believe I've to send(post) the form data through ajax to django views where my form will be saved(files will be saved)
– user10058776
Jan 22 at 7:31
|
show 9 more comments
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54259993%2fhow-to-send-django-multiple-filefields-form-data-through-ajax%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54259993%2fhow-to-send-django-multiple-filefields-form-data-through-ajax%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
Have you checked the answers in: stackoverflow.com/questions/2320069/jquery-ajax-file-upload ?
– schillingt
Jan 18 at 19:46
@schillingt I don't want to upload file through ajax, I want to POST the list of input files and file uploading will be done in the django views. I tried to pass them in the data attribute of ajax.I've attached the code, will you please check out and tell me the correct code?
– user10058776
Jan 19 at 7:26
@ThomasJiang can you answer this?
– user10058776
Jan 19 at 9:50
You're making an ajax call to submit the form but you don't want to upload the file through ajax? Can you elaborate what you're trying to do? Your form is expecting to receive files (including the file data) so what do you mean by "file uploading will be done in the django view?" And maybe show us your
CreatePost
view to see how you handle the ajax post.– dirkgroten
Jan 21 at 10:04
@dirkgroten I've given everything now in the edited question. I don't know anything about ajax, but this task required me to use it. I believe I've to send(post) the form data through ajax to django views where my form will be saved(files will be saved)
– user10058776
Jan 22 at 7:31