HTML:
<form enctype="multipart/form-data" action="/convert_upl" method="post">
Name: <input type="text" name="file_name">
File: <input type="file" name="subs_file">
<input type="submit" value="Send">
</form>
Python (Google App Engine):
if self.request.get('file_name'):
file_name = self.request.get('file_name')
My problem is that I receive no data from file_name text input. I am aware that the trouble is because of it's existence within the form enctype="multipart/form-data" but I don't know how to solve it - I mean how to receive a file and the string from the input with one click of the submit button.
Thanks in advance.
The uploading example code works fine for me. Have you tried using that code exactly? Does it work for you, or what problems do you see?
As you'll see, that example has a form with the same encoding you're using:
<form action="/sign" enctype="multipart/form-data" method="post">
<div><label>Message:</label></div>
<div><textarea name="content" rows="3" cols="60"></textarea></div>
<div><label>Avatar:</label></div>
<div><input type="file" name="img"/></div>
<div><input type="submit" value="Sign Guestbook"></div>
</form>
it's just a bit more careful in the HTML to properly use label tags to display field labels, but that only affect the form's looks when rendered in the browser.
The Python code is also similar to what you show (for the tiny susbset that you do show):
def post(self):
greeting = Greeting()
if users.get_current_user():
greeting.author = users.get_current_user()
greeting.content = self.request.get("content")
avatar = self.request.get("img")
greeting.avatar = db.Blob(avatar)
greeting.put()
self.redirect('/')
and of course the /sign URL is directed to the class whose do_post method we've just shown.
So, if this code works and yours doesn't, where is the difference? Not in the part you've shown us, so it must be in some parts you haven't shown... can you reproduce the part about this example code from Google working just fine?
You are using the POST method to send the data but then are trying to get it with the GET method.
instead of
self.request.get('file_name')
do something like
self.request.post('file_name')
Related
I am trying to capture HTTP form data using Python and Flask, but I keep getting "method not allowed" error. I have been trying to figure out why for some time with no luck. Here is what my code looks like:
#app.route("/add_recipe")
def addrecipepage():
if request.method=="post":
print ("Successful post request") # Just testing if code is working so far
return render_template("add_recipe.html")
and the HTML code:
<form id="contact" action="browseAll" method="post">
<div class="row">
<div class="col-md-6">
<fieldset>
<input name="name" type="text" class="form-control" id="name" placeholder="Recipe Name..." required="">
</fieldset>
<div class="col-12">
<textarea name="demo-message" id="recipeText" placeholder="Enter Ingredients & Instructions Here" rows="15"></textarea>
</div>
<div class="col-md-12">
<button type="submit" id="form-submit" class="button">Add The Recipe!</button>
<button id="uploadPhotoButton">Upload A Photo!</button>
</div>
</form>
When I click on the submit button, I get a HTTP 405 Method Not Allowed error. Anybody have any ideas as to why after looking at this? "browseAll" is another HTML page that I created an endpoint for in my app.py file. The same error gets thrown even if I don't specify an action though.
You are doing a POST (your form has method = "post") but your route does not have a post method attached to it. When you define a route without attaching a method, it defaults to GET. You need to do this
#app.route("/add_recipe", methods =['GET', 'POST'])
So, last few days I was trying to build a website where you input the post data then hit submit and then boom your post appears on the home page/post page.
So, I have created the form but the problem is that I don't know how to send HTML form data to SQLite database so it can be viewed by multiple users anytime.
<form class="posts" action="." method="post">
<h2>Title</h2>
<input type="text" name="title" placeholder="Title">
<h2>Description</h2>
<textarea input class="des" name="description"type="text" placeholder="Description"></textarea>
<h2>Image(Optional)</h2>
<input type="file" name="inpFile" id="inpFile" class="img-btn">
<div class="img-prev" id="imgPrev">
<img src="" alt="Image Preview" class="img-prev__img">
<span class="img-prev__def-text">Image Preview</span>
</div>
<input type="submit" value="Submit">
</form>
I think that you should take a look at some Python's web-frameworks like Flask or Django first, so that you can understand this subject a little clearer.
I can't get an input field in a form to send a value to the server. (I'm using flask + python on Google App Engine).
Please excuse me if this is a rookie question...
Part of my html template file:
<form class="form-inline" action="/rm_list" method="POST">
<div class="row" >
<div class="col-span-6">
<fieldset>
<input class="form-control form-control-lg" type="text" name="searchtext" placeholder="Product...">
<input name="text1">
<button class="btn btn-primary" type="submit"><i class="material-icons w3-text-black" >search</i></button>
</div>
...
...
some radio buttons
So I dumped the POST data to the terminal to debug:
my_data = request.form
for key in my_data:
print ('form key '+key+" "+my_data[key])
After trying different solutions, I found that the culprit is the type="text" attribute.
I could see the value of the simple text1 input, but the value from the searchtext input just wasn't in the received data :-(
If I remove "type="text" as follows:
<input class="form-control form-control-lg" name="searchtext" Placeholder="Product...">
then the searchtext field is received Ok by the server.
Any ideas what I'm doing wrong?
Thanks!
It seems to me that your field is simply missing the value attribute. – Anonymous
I'm working with django and I want to calculate something depending on what the user put on the selectboxes of my page, but I don't know how to connect that with my python code that calculates everything, please help :(
You can use a html form element like this with method post and action="path_of_your_python_file"
<form name="search" action="calculate.py" method="GET">
Search: <input type="checkbox" name="user_input">
<input type="submit" value="Submit">
</form>
Then in calculate.py
import cgi
form = cgi.FieldStorage()
searchterm = form.getvalue('user_input')
// Do your calculation here
I think that will help
I'm wondering why this is happening:
I'm sending file data with form in POST, but all the time it says:
Key 'file' not found in <QueryDict:
{
u'datum': [u'aaa'],
u'csrfmiddlewaretoken': [u'USAbRrgU92yj7KFpZHuxf9bWufgnwC4N'],
u'anzeige': [u' aaaa'],
u'titel': [u' aaa']
}
This is my html:
<form id="myform" action="/anzeige_save/" method="post" enctype="multipart/form-data">
{% csrf_token %}
<textarea style="width: 450px" id="titel" name="titel"> </textarea>
<textarea name="anzeige" id="anzeige"
style="height: 180px; width: 450px"> </textarea>
<input type="text" id="datum" name="datum" >
<input type="file" id="file" name="file" size="40" maxlength="100000">
<input type="button" value="speichern"
onclick="javascript:submitform()" />
</form>
<script>
function submitform(){
document.forms["myform"].submit();
}
</script>
and part my view is this:
anzeige=Anzeige(titel=request.POST['titel'],
anzeige=end_anzeige,
date=datetime.datetime.now(),
datum=request.POST['datum'],
file=request.FILES['file'])
anzeige.save()
I'm actually submitting correctly, the form should also submit the file in QueryDict like other queries. Can someone help me to figure out what I'm missing here?
Thanks
Use a ModelForm instead of a form, unless you have a good reason not to.
Your HTML will display a blank form if the form doesn't validate, without mentioning the errors nor previously entered values, see customizing a form template for correct usage.
It is normal that request.POST['file'] does not exist, it's in request.FILES['file'], but it must be saved before it can be used in a model. If you use a ModelForm, it is automatic. Else, see handling uploaded files with a model.