How can I send a file created by javascript, to server and then handle it by django, for emailing?
My javascript code:
function PrepareEmailPrescription() {
doc = PreparePrescriptionPDF()
var pdf = doc.output();
var fd = new FormData();
fd.append( 'file', pdf );
$.ajax({
url: `/clinic/${cliniclabel}/prescription/sendemail/patient/${patient_id}`,
data: fd,
processData: false,
contentType: false,
type: 'POST',
success: function(data){
alert(data);
}
});
}
Here, PreparePrescriptionPDF is a function which generated a pdf file with pdfjs.
On the django server, I have:
def SendPrescriptionbyMail(request, cliniclabel, patient_id):
# print(request.body)
print(request.FILES)
print(request.FILES['file'])
return HttpResponse('Successfully sent email')
I am getting the following response from django:
[14/Nov/2018 15:59:18] "GET /clinic/medicines HTTP/1.1" 200 8389
<MultiValueDict: {}>
2018-11-14 15:59:22,344 django.request ERROR Internal Server Error: /clinic/madhav/prescription/sendemail/patient/18
Traceback (most recent call last):
File "/home/joel/myappointments/venv/lib/python3.6/site-packages/django/utils/datastructures.py", line 77, in __getitem__
list_ = super().__getitem__(key)
KeyError: 'file'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/joel/myappointments/venv/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/home/joel/myappointments/venv/lib/python3.6/site-packages/django/core/handlers/base.py", line 126, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/joel/myappointments/venv/lib/python3.6/site-packages/django/core/handlers/base.py", line 124, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/joel/myappointments/clinic/views.py", line 4911, in SendPrescriptionbyMail
print(request.FILES['file'])
File "/home/joel/myappointments/venv/lib/python3.6/site-packages/django/utils/datastructures.py", line 79, in __getitem__
raise MultiValueDictKeyError(key)
django.utils.datastructures.MultiValueDictKeyError: 'file'
However if my javascript code becomes:
function PrepareEmailPrescription() {
doc = PreparePrescriptionPDF()
var pdf = new Blob(['abc123'], {type: 'text/plain'});
var fd = new FormData();
fd.append( 'file', pdf );
$.ajax({
url: `/clinic/${cliniclabel}/prescription/sendemail/patient/${patient_id}`,
data: fd,
processData: false,
contentType: false,
type: 'POST',
success: function(data){
alert(data);
}
});
}
I get:
[14/Nov/2018 15:57:05] "GET /clinic/medicines HTTP/1.1" 200 8389
<MultiValueDict: {'file': [<InMemoryUploadedFile: blob (text/plain)>]}>
2018-11-14 15:57:10,500 django.request ERROR Internal Server Error: /clinic/madhav/prescription/sendemail/patient/18
Traceback (most recent call last):
File "/home/joel/myappointments/venv/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/home/joel/myappointments/venv/lib/python3.6/site-packages/django/utils/deprecation.py", line 93, in __call__
response = self.process_response(request, response)
File "/home/joel/myappointments/venv/lib/python3.6/site-packages/django/middleware/common.py", line 105, in process_response
if response.status_code == 404:
AttributeError: 'str' object has no attribute 'status_code'
What's the proper way to do this? How can I send a on-the-fly-created pdf file to my django server for emailing as an attachment?
javascript python ajax django
add a comment |
My javascript code:
function PrepareEmailPrescription() {
doc = PreparePrescriptionPDF()
var pdf = doc.output();
var fd = new FormData();
fd.append( 'file', pdf );
$.ajax({
url: `/clinic/${cliniclabel}/prescription/sendemail/patient/${patient_id}`,
data: fd,
processData: false,
contentType: false,
type: 'POST',
success: function(data){
alert(data);
}
});
}
Here, PreparePrescriptionPDF is a function which generated a pdf file with pdfjs.
On the django server, I have:
def SendPrescriptionbyMail(request, cliniclabel, patient_id):
# print(request.body)
print(request.FILES)
print(request.FILES['file'])
return HttpResponse('Successfully sent email')
I am getting the following response from django:
[14/Nov/2018 15:59:18] "GET /clinic/medicines HTTP/1.1" 200 8389
<MultiValueDict: {}>
2018-11-14 15:59:22,344 django.request ERROR Internal Server Error: /clinic/madhav/prescription/sendemail/patient/18
Traceback (most recent call last):
File "/home/joel/myappointments/venv/lib/python3.6/site-packages/django/utils/datastructures.py", line 77, in __getitem__
list_ = super().__getitem__(key)
KeyError: 'file'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/joel/myappointments/venv/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/home/joel/myappointments/venv/lib/python3.6/site-packages/django/core/handlers/base.py", line 126, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/joel/myappointments/venv/lib/python3.6/site-packages/django/core/handlers/base.py", line 124, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/joel/myappointments/clinic/views.py", line 4911, in SendPrescriptionbyMail
print(request.FILES['file'])
File "/home/joel/myappointments/venv/lib/python3.6/site-packages/django/utils/datastructures.py", line 79, in __getitem__
raise MultiValueDictKeyError(key)
django.utils.datastructures.MultiValueDictKeyError: 'file'
However if my javascript code becomes:
function PrepareEmailPrescription() {
doc = PreparePrescriptionPDF()
var pdf = new Blob(['abc123'], {type: 'text/plain'});
var fd = new FormData();
fd.append( 'file', pdf );
$.ajax({
url: `/clinic/${cliniclabel}/prescription/sendemail/patient/${patient_id}`,
data: fd,
processData: false,
contentType: false,
type: 'POST',
success: function(data){
alert(data);
}
});
}
I get:
[14/Nov/2018 15:57:05] "GET /clinic/medicines HTTP/1.1" 200 8389
<MultiValueDict: {'file': [<InMemoryUploadedFile: blob (text/plain)>]}>
2018-11-14 15:57:10,500 django.request ERROR Internal Server Error: /clinic/madhav/prescription/sendemail/patient/18
Traceback (most recent call last):
File "/home/joel/myappointments/venv/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/home/joel/myappointments/venv/lib/python3.6/site-packages/django/utils/deprecation.py", line 93, in __call__
response = self.process_response(request, response)
File "/home/joel/myappointments/venv/lib/python3.6/site-packages/django/middleware/common.py", line 105, in process_response
if response.status_code == 404:
AttributeError: 'str' object has no attribute 'status_code'
What's the proper way to do this? How can I send a on-the-fly-created pdf file to my django server for emailing as an attachment?
javascript python ajax django
What is the type of doc.output() ? Console logging this would be useful information.
– forgetso
Nov 14 '18 at 11:14
you get an error indjango
code either way. Are you sure you do not miss something else?
– Nikos M.
Nov 14 '18 at 12:05
add a comment |
My javascript code:
function PrepareEmailPrescription() {
doc = PreparePrescriptionPDF()
var pdf = doc.output();
var fd = new FormData();
fd.append( 'file', pdf );
$.ajax({
url: `/clinic/${cliniclabel}/prescription/sendemail/patient/${patient_id}`,
data: fd,
processData: false,
contentType: false,
type: 'POST',
success: function(data){
alert(data);
}
});
}
Here, PreparePrescriptionPDF is a function which generated a pdf file with pdfjs.
On the django server, I have:
def SendPrescriptionbyMail(request, cliniclabel, patient_id):
# print(request.body)
print(request.FILES)
print(request.FILES['file'])
return HttpResponse('Successfully sent email')
I am getting the following response from django:
[14/Nov/2018 15:59:18] "GET /clinic/medicines HTTP/1.1" 200 8389
<MultiValueDict: {}>
2018-11-14 15:59:22,344 django.request ERROR Internal Server Error: /clinic/madhav/prescription/sendemail/patient/18
Traceback (most recent call last):
File "/home/joel/myappointments/venv/lib/python3.6/site-packages/django/utils/datastructures.py", line 77, in __getitem__
list_ = super().__getitem__(key)
KeyError: 'file'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/joel/myappointments/venv/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/home/joel/myappointments/venv/lib/python3.6/site-packages/django/core/handlers/base.py", line 126, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/joel/myappointments/venv/lib/python3.6/site-packages/django/core/handlers/base.py", line 124, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/joel/myappointments/clinic/views.py", line 4911, in SendPrescriptionbyMail
print(request.FILES['file'])
File "/home/joel/myappointments/venv/lib/python3.6/site-packages/django/utils/datastructures.py", line 79, in __getitem__
raise MultiValueDictKeyError(key)
django.utils.datastructures.MultiValueDictKeyError: 'file'
However if my javascript code becomes:
function PrepareEmailPrescription() {
doc = PreparePrescriptionPDF()
var pdf = new Blob(['abc123'], {type: 'text/plain'});
var fd = new FormData();
fd.append( 'file', pdf );
$.ajax({
url: `/clinic/${cliniclabel}/prescription/sendemail/patient/${patient_id}`,
data: fd,
processData: false,
contentType: false,
type: 'POST',
success: function(data){
alert(data);
}
});
}
I get:
[14/Nov/2018 15:57:05] "GET /clinic/medicines HTTP/1.1" 200 8389
<MultiValueDict: {'file': [<InMemoryUploadedFile: blob (text/plain)>]}>
2018-11-14 15:57:10,500 django.request ERROR Internal Server Error: /clinic/madhav/prescription/sendemail/patient/18
Traceback (most recent call last):
File "/home/joel/myappointments/venv/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/home/joel/myappointments/venv/lib/python3.6/site-packages/django/utils/deprecation.py", line 93, in __call__
response = self.process_response(request, response)
File "/home/joel/myappointments/venv/lib/python3.6/site-packages/django/middleware/common.py", line 105, in process_response
if response.status_code == 404:
AttributeError: 'str' object has no attribute 'status_code'
What's the proper way to do this? How can I send a on-the-fly-created pdf file to my django server for emailing as an attachment?
javascript python ajax django
My javascript code:
function PrepareEmailPrescription() {
doc = PreparePrescriptionPDF()
var pdf = doc.output();
var fd = new FormData();
fd.append( 'file', pdf );
$.ajax({
url: `/clinic/${cliniclabel}/prescription/sendemail/patient/${patient_id}`,
data: fd,
processData: false,
contentType: false,
type: 'POST',
success: function(data){
alert(data);
}
});
}
Here, PreparePrescriptionPDF is a function which generated a pdf file with pdfjs.
On the django server, I have:
def SendPrescriptionbyMail(request, cliniclabel, patient_id):
# print(request.body)
print(request.FILES)
print(request.FILES['file'])
return HttpResponse('Successfully sent email')
I am getting the following response from django:
[14/Nov/2018 15:59:18] "GET /clinic/medicines HTTP/1.1" 200 8389
<MultiValueDict: {}>
2018-11-14 15:59:22,344 django.request ERROR Internal Server Error: /clinic/madhav/prescription/sendemail/patient/18
Traceback (most recent call last):
File "/home/joel/myappointments/venv/lib/python3.6/site-packages/django/utils/datastructures.py", line 77, in __getitem__
list_ = super().__getitem__(key)
KeyError: 'file'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/joel/myappointments/venv/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/home/joel/myappointments/venv/lib/python3.6/site-packages/django/core/handlers/base.py", line 126, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/joel/myappointments/venv/lib/python3.6/site-packages/django/core/handlers/base.py", line 124, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/joel/myappointments/clinic/views.py", line 4911, in SendPrescriptionbyMail
print(request.FILES['file'])
File "/home/joel/myappointments/venv/lib/python3.6/site-packages/django/utils/datastructures.py", line 79, in __getitem__
raise MultiValueDictKeyError(key)
django.utils.datastructures.MultiValueDictKeyError: 'file'
However if my javascript code becomes:
function PrepareEmailPrescription() {
doc = PreparePrescriptionPDF()
var pdf = new Blob(['abc123'], {type: 'text/plain'});
var fd = new FormData();
fd.append( 'file', pdf );
$.ajax({
url: `/clinic/${cliniclabel}/prescription/sendemail/patient/${patient_id}`,
data: fd,
processData: false,
contentType: false,
type: 'POST',
success: function(data){
alert(data);
}
});
}
I get:
[14/Nov/2018 15:57:05] "GET /clinic/medicines HTTP/1.1" 200 8389
<MultiValueDict: {'file': [<InMemoryUploadedFile: blob (text/plain)>]}>
2018-11-14 15:57:10,500 django.request ERROR Internal Server Error: /clinic/madhav/prescription/sendemail/patient/18
Traceback (most recent call last):
File "/home/joel/myappointments/venv/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/home/joel/myappointments/venv/lib/python3.6/site-packages/django/utils/deprecation.py", line 93, in __call__
response = self.process_response(request, response)
File "/home/joel/myappointments/venv/lib/python3.6/site-packages/django/middleware/common.py", line 105, in process_response
if response.status_code == 404:
AttributeError: 'str' object has no attribute 'status_code'
What's the proper way to do this? How can I send a on-the-fly-created pdf file to my django server for emailing as an attachment?
javascript python ajax django
javascript python ajax django
edited Nov 15 '18 at 2:26
Joel G Mathew
asked Nov 14 '18 at 10:36
Joel G MathewJoel G Mathew
2,05792745
2,05792745
What is the type of doc.output() ? Console logging this would be useful information.
– forgetso
Nov 14 '18 at 11:14
you get an error indjango
code either way. Are you sure you do not miss something else?
– Nikos M.
Nov 14 '18 at 12:05
add a comment |
What is the type of doc.output() ? Console logging this would be useful information.
– forgetso
Nov 14 '18 at 11:14
you get an error indjango
code either way. Are you sure you do not miss something else?
– Nikos M.
Nov 14 '18 at 12:05
What is the type of doc.output() ? Console logging this would be useful information.
– forgetso
Nov 14 '18 at 11:14
What is the type of doc.output() ? Console logging this would be useful information.
– forgetso
Nov 14 '18 at 11:14
you get an error in
django
code either way. Are you sure you do not miss something else?– Nikos M.
Nov 14 '18 at 12:05
you get an error in
django
code either way. Are you sure you do not miss something else?– Nikos M.
Nov 14 '18 at 12:05
add a comment |
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%2f53298192%2fhow-can-i-send-a-file-created-by-javascript-to-server-and-then-handle-it-by-dja%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%2f53298192%2fhow-can-i-send-a-file-created-by-javascript-to-server-and-then-handle-it-by-dja%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
What is the type of doc.output() ? Console logging this would be useful information.
– forgetso
Nov 14 '18 at 11:14
you get an error in
django
code either way. Are you sure you do not miss something else?– Nikos M.
Nov 14 '18 at 12:05