I tried to upload two files woth two different location but when uploading files, I'm getting this error "ValueError: View function did not return a response". I tried everything to get it works but no luck.
html code:
<form action="/NewCases/" method=post class="form-horizontal">
<h2>Add New Cases: </h2>
<div class="input-group">
<span class="input-group-addon">#</span>
<input type="text" id="casename" name="casename" class="form-control"
placeholder="Enter Case Name:" required>
</div>
<div class="input-group">
<span class="input-group-addon">#</span>
<input type="text" id="adminname" name="adminname" class="form-control"
placeholder="Enter Case Name:" value="{{ current_user.username }}">
</div>
<div class="input-group">
<span class="input-group-addon">#</span>
<input type="file" id="imagefile" name="imagefile" class="form-control"
placeholder="Enter Hard Disk File:" required>
</div>
<div class="input-group">
<span class="input-group-addon">#</span>
<input type="file" id="memimagefile" name="memimagefile" class="form-control"
placeholder="Enter Memory File:" required>
</div>
<div class="control-group">
<div class="controls">
<button type="submit" class="btn btn-success">Signup</button>
</div>
</div>
</form>
flask code
#app.route('/NewCases/', methods=['GET', 'POST'])
def NewCase():
try:
if request.method == 'GET':
return render_template('admin.html', DICT=DICT)
if request.method == 'POST':
idtest = str(uuid.uuid4())
idtestfilter = idtest.replace('-','')
adname= request.form['adminname']
casen = request.form['casename']
imagefile = request.files['imagefile']
memimagefile =request.files['memimagefile']
if imagefile > 0:
imagefilename = secure_filename(imagefile.filename)
file.save(os.path.join(UPLOAD_FOLDER , imagefilename))
return redirect(url_for('index'))
if memimagefile > 0:
imagefilename = secure_filename(memimagefile.filename)
file.save(os.path.join(UPLOAD_FOLDER , memimagefile))
return redirect(url_for('index'))
c, conn = connection()
c.execute("INSERT INTO cases(id,casename, adminname, imagepath, memimagepath) VALUES (%s, %s, %s, %s, %s)",
(thwart(idtestfilter),thwart(adname),thwart(casen),thwart(imagefilename),thwart(memfilename)))
conn.commit()
flash("case inserted!")
c.close()
conn.close()
flash('Cases successfully added')
return redirect(url_for('EditCase'))
except Exception as e:
error = e
Please help me out!
The first thing to point out is that your Python code is failing, but you are catching and ignoring all exceptions (well, almost all). The main reason that your Python code is failing is because a bad request is being received. You would know that if you did not catch and ignore all exceptions. At least print a message, and raise the exception again.
Anyway, you are uploading files so you need to set the encoding type for your HTML form to multipart/form-data, like this:
<form action="http://127.0.0.1:5000/" method=post enctype="multipart/form-data" class="form-horizontal">
That's the cause of the bad request.
I assume that you have imported all required modules and functions such as uuid, secure_filename etc., however, you are not quite saving the files properly. Use imagefile.save() and memimagefile.save(), not file.save().
Also, because both files can be uploaded at the same time, and because there is database code that should be run, you should not return after saving the files.
Finally, your code assumes that the form fields will always be present in the form. If one is missing your code will fail with a KeyError.
Related
I wrote a simple form. For some reason, the form sends always GET requests instead of POST.
CODE EXAMPLE
<form action="{{ url_for('home') }}" method="post">
<div class="row">
<div class="col-sm-12">
<input type="text" required dir="rtl" name="name" class="form-control" id="Name" placeholder=" שם ">
</div>
<div class="col-sm-12">
<input type="tel" required dir="rtl" name="phone" class="form-control" id="Phone" placeholder=" פלאפון ">
</div>
<div class="col-sm-12">
<input type="email" dir="rtl" name="email" class="form-control" id="Email" placeholder=" דוא'ל ">
</div>
<div class="col-sm-12">
<textarea class="form-control" dir="rtl" name="text" rows="5" id="Message" placeholder=" הודעה "></textarea>
</div>
<div class="col-sm-12 col-md-12 c-c-padding">
<button type="submit" name="send" id="contact_submit" class="btn btn-dm"> שלח </button>
</div>
</div>
</form>
SERVER SIDE
#app.route('/', methods=["GET", "POST"])
def home():
print(request.method)
if request.method == 'POST':
name = request.form['name']
phone = request.form['phone']
email = request.form['email']
print(name + phone + email)
if phone:
requests.post('DOMAIN',
auth=("api", 'API'),
data={"from": "Email",
"to": ["email"],
"bcc": "",
"subject": " ",
"text": """ Name: {}
Phone: {}
Email: {} """.format(name, phone, email)})
flash(' welldone ', 'success')
return redirect(url_for('home'))
else:
flash(' phone ', 'warning')
return redirect(url_for('home'))
return render_template('index.html')
After I press the 'submit' button. As I said, it sends GET request instead of a POST.
Then something weird happens. It injects 'index.html' into the form - and what happens, is that I have a website inside the website.
First, what should I do to fix the problem of sending POST request instead of GET request.
Second, why 'render_template()' injects the HTML into the form? Instead render the whole page?
Replace
return redirect(url_for('home'))
with
return redirect(request.url)
or
return redirect(request.referrer)
Why? If we read the docstring for url_for():
Generates a URL to the given endpoint with the method provided
We see that redirect(url_for('home')) tells Flask to redirect the client to the home() endpoint, which then calls redirect(url_for('home')) and so on in a looping cycle.
When needed, call redirect(url_for('home')) from endpoints different than home().
From the docstrings for request.url and request.referrer:
request.url :
The full request URL with the scheme, host, root path, path,
and query string.
request.referrer :
The Referer[sic] request-header field allows the client
to specify, for the server's benefit, the address (URI) of the
resource from which the Request-URI was obtained (the
"referrer", although the header field is misspelled).
You can read more about them from these SO answers.
Getting a 400, when trying to upload a file ad send other form elements to flask from html. Tried to use ajax, but that throws me an error as well.
Python:
#app.route('/prod_diff_result', methods=['POST', 'GET'])
def prod_diff_result():
try:
host = request.form['host-prod-iterator']
print(host)
if request.files['file']:
f = request.files['file']
f.save(secure_filename(f.filename))
HTML:
<div class="main-div">
<form action="/prod_diff_result" method="POST" enctype="multipart/form-data">
<div class="grid-container">
<div class="grid-item">
<span class="label label-default ">PROD</span><br>
<p>Iterator Host : <input type="text" class="form-control" id="host-prod-iterator" value="10.47.7.57"
required></p>
<input type="radio" name="data_fetch_type" value="file" onclick="showfile()">Upload File
<input type="file" name="file" />
<input type="radio" name="data_fetch_type" value="db"> Get from DB
<input type="submit" />
</div>
</form>
</div>
I want to be able send hostname and file back to flask error in one request and using one form.
It gives an error because you try to access a form field that it cannot find, and assumes that somehow the request was bad, because it didn't include a required form field. You are trying to access:
host = request.form['host-prod-iterator']
However you have simply not given it a name in your HTML. If you give it a name, it should work:
<p>Iterator Host :
<input type="text" class="form-control" name="host-prod-iterator" id="host-prod-iterator" value="10.47.7.57" required>
</p>
I'm in the process of writing a test for the login portion of an app I'm creating in Flask. However, when I pass data using the test_client.post() method my test data isn't being passed correctly.
What's more, the manual login test POSTs and redirects fine.
The test case code is:
# Ensure Login behaves correctly given the correct credentials
def test_correct_login(self):
tester = app.test_client(self)
response = tester.post('/login',data= dict(username = 'bigtest', password = 'testing2'), follow_redirects = True, content_type='application/x-www-form-urlencoded')
self.assertIn(b'Section title', response.data)
The response given shows that the username is passed correct, but the password does not have a value.
This is the response returned in console:
<div class="form-group required">
<label class="control-label" for="username">username</label>
<input class="form-control" id="username" name="username" required type="text" value="bigtest">
</div>
<div class="form-group required">
<label class="control-label" for="password">password</label>
<input class="form-control" id="password" name="password" required type="password" value="">
I'm not sure what the issue is here. Has this happened to anyone else before?
I want to upload an image to twitter taken by a django form:
<div class="panel panel-danger">
<div class="panel-heading">
<h3 class="panel-title">Panel title</h3>
</div>
<div class="panel-body">
<form action="" method="POST" role="form" enctype="multipart/form-data">
{% csrf_token %}
<legend>Upload a file: </legend>
<div class="form-group">
<input type="file" name="file" class="form-control" id="">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
</div>
The image I got is:
if fileForm.is_valid():
print "file is uploaded."
paramFile = open(request.FILES['file'],'rb')
# paramFile = request.FILES['file'].read()
api.update_with_media(paramFile,status="Hello first image")
The error I got is:
coercing to Unicode: need string or buffer, InMemoryUploadedFile found
How can I upload this to twitter?
Method update_with_media() has only one positional argument which takes filename.
So you can specify filename something like this:
api.update_with_media(request.FILES['file'].name,
status="Hello first image")
Also you should pass file using keyword argument 'file':
api.update_with_media(request.FILES['file'].name,
file=request.FILES['file'],
status="Hello first image")
As per docs, you have to pass both file parameter which will be opened internally and filename parameter which is needed to determine MIME type and will be used to as form field in post data. So just pass them explicitly as keyword arguments and you should be fine.
Trying to upload multiple files at once using python. The upload.html source code is as followed:
<form name="frmRegister" method="post" accept-charset="utf-8" enctype="multipart/form-data" class="form-horizontal">
<div class="control-group">
<div class="controls">
<input type="file" name="files" multiple='multiple'>
</div>
</div>
<div class="control-group">
<div class="controls">
<input class="btn btn-primary" type="submit" name="btnSubmit" value="Add Product" />
</div>
</div>
</form>
in my admin.py:
#view_config(context="mycart:resources.Product", name="add", renderer='admin/mall/product/add.jinja2', permission = 'admin')
#view_config(context="mycart:resources.Product", name="add", request_method="POST", renderer='admin/mall/product/add.jinja2', permission = 'admin')
def product_add(context, request):
if 'btnSubmit' in request.POST:
print ("files >>> ", request.POST['files'])
in my terminal, it is showing just FieldStorage('files', u'DSC01973.JPG') whereas I've selected 'DSC01975.JPG', 'DSC01976.JPG'.
Why is this so?
I've found a way to solve it, I believe there are many others, if there are, please feel free to holler out:
fileslist = request.POST.getall('files')
print ("My files listing: ", fileslist)
for f in fileslist:
print ( "individual files: ", f )
I could solve the problem with the following function:
from cgi import FieldStorage
def get_all_file_data_list(request):
return [x for x in request.POST.values() if isinstance(x, FieldStorage)]