how do take the user input with flask? [duplicate] - python

This question already has answers here:
Sending data from HTML form to a Python script in Flask
(2 answers)
Closed 2 years ago.
I am working on a web GUI for a program I wrote the first thing a user of this software will access is the default login page for the program with that, said how do you take the username and password that the user-provided and validate it
how do you take the input from the user?
this is the login page in the flask script
#app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
if request.form['log'] == "Log In Here":
u_name = request.form['Uname']
pass_wrd = request.form['Pass']
else:
pass
print ( u_name , pass_wrd)
return
else:
return render_template('login-form.html')
this is the HTML side for the login page
<form id="login" method="get" action="login.php">
<label><b>User Name
</b>
</label>
<input type="text" name="Uname" id="Uname" placeholder="Username">
<br><br>
<label><b>Password
</b>
</label>
<input type="Password" name="Pass" id="Pass" placeholder="Password">
<br><br>
<input type="button" name="log" id="log" value="Log In Here">
<br><br>
<input type="checkbox" id="check">

if request.form["log"] == "Log In Here
You have literally used the method here ^
request.form['name'] will get values from the input tag with name = 'name' (you can change that to catch the input values you want)
Store the value in variable and authorize the user with username and password from your database.

Related

Bootstrap 4 input field not sending value to server (python, flask, GAE)

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

Python : How to submit CGI form using Request

I just start learning Python and want to make a script to submit Form.
I found Form use CGI, Here the Form:
<div class="box" id="url_upload">
<div class="tabcontent">
<div class="progress_div"></div>
<div class="reurlupload">
<div class="progress_div"></div>
<form method="post" id="uploadurl" action="https://af03.ayefiles.com/cgi-bin/upload.cgi?upload_type=url">
<input type="hidden" name="sess_id" value="xv71zsrmtr38oh3z">
<input type="hidden" name="utype" value="reg">
<input type="hidden" name="file_public" value="1">
<div class="leftintab">
<p style="margin:0px;">
You can enter up to <b>20</b> URLs, one URL per row</br>
Max file size is <b>10240 Mb</b>
</p>
<textarea name="url_mass" style="width:100%; margin-top: 10px;" placeholder="e.g. http://example.com/xxxxxxxxxx.xyz"></textarea>
</div>
I make python script using request as below:
#I have session with my login & password as cookie
#Go to form page
login = s.get('https://ayefiles.com/?op=upload_form')
login_html = html.fromstring(login.content)
hidden_inputs = login_html.xpath('//input[#type="hidden"]')
# Input query data
form = {x.attrib["name"]: x.attrib["value"] for x in hidden_inputs}
form ['sess_id']= 'xv71zsrmtr38oh3z'
form['utype']= 'reg'
form ['file_public']= '1'
form['url_mass'] = longurl
# POST
login = s.post('https://af03.ayefiles.com/cgi-bin/upload.cgi?upload_type=url', data=form)
print (login.url)
My expected result for login.url ==> ayefiles.com/?op=upload_result&st=OK&fn=xxxxx
But my result fail, ==> ayefiles.com/?op=upload_result&st=Torrent%20engine%20is%20not%20running&
fn=undef
how to solve my problem? What's wrong with my code?
Please kindly help me with correct code.
My mistake at part multiform data.
Correct code :
form ={'sess_id':(None,'xv71zsrmtr38oh3z'),'utype':(None,'reg'),'file_public':(None,'1'),'url_mass':(None,longurl)}
login = s.post('https://af03.ayefiles.com/cgi-bin/upload.cgi?upload_type=url', data=form)

Flask Bad Request Error; ocurring because of multiple POST requests [duplicate]

This question already has answers here:
Why dict.get(key) instead of dict[key]?
(14 answers)
Closed 4 years ago.
<form method = "POST">
<div class=" col-lg-4 col-lg-4 col-lg-4 col-lg-4">
<div class="box">
<input type="text" name="image-url" placeholder="Image URL Link"style="color:black" required="required" value = "new"/>
<textarea cols=80 rows=4 style="color:black" name = "description" placeholder="Place your description here" value = "new"></textarea>
<button type="submit">Upload</button>
</div>
</div>
</form>
{% for i in range(amount_of_images) %}
<div class=" col-lg-4 col-lg-4 col-lg-4 col-lg-4">
<div class="box">
<img src="{{image[i]}}" alt="view" width = "300" height = "300"/>
<form method = "POST">
<textarea cols=80 rows=4 style="color:black" name = "update-description" value = "update">{{description[i]}}</textarea>
<button type="submit">Update Description</button>
</form>
Above is my HTML/Jinja Code
#app.route("/gallery-manager", methods = ["GET", "POST"])
def gallery_manager():
if request.method == "POST":
if(request.form["image-url"] and request.form["description"]) is not None:
model.add_new_image(request.form["image-url"], request.form["description"])
id, image, description, amount_of_images = model.get_gallery()
return render_template("gallery-manager.html", image = image, description = description, amount_of_images = amount_of_images, id = id)
if request.form['update-description'] is not None:
print("hello")
id, image, description, amount_of_images = model.get_gallery()
return render_template("gallery-manager.html", image = image, description = description, amount_of_images = amount_of_images, id = id)
Above is my Python/Flask Code...
The issue, when I click on the update-description submit button, aka the second POST being handled in the html code, I get a 400 error
Bad Request
The browser (or proxy) sent a request that this server could not understand.
I realized that this error occurs when one of the POST fields are empty and can not be found. I understand that this happens because when I click the second POST submit button, it runs through the first POST check (request.form["image-url"] etc and finds that it is satisfied therefore wanting to run that code, but cant because update-description is still empty. How do I avoid this.
In other words how do I handle multiple POST methods.
Thank you,
This is because you are missing the action section in form where the form should send the request
<form action="/whereYouWantToSendRequest" method="post">
add your url endpoint by replacing this in the above
whereYouWantToSendRequest
here are the two request
<input type="text" name="image-url" value = "new"/>
<input type="text" name="update-description" value = "update"/>
and to figure out which request
if request.form["image-url"] == "new":
somethinggg
elif request.form["update-description"] =="update":
sommmm

How to authenticate the user before going to other script in python

I am new to python, I am trying to built a website with python, which is having 2 webpages.
'login.html'
'details.html'
But the user directly entering to details page without login also, how can I stop this?
I am able to do it when I am working with django using login_required, but when I am using cgi-bin script python, for webportal How can I stop this?
Please help me on this.
Here is the sample code:
login.html
<form action = "login1.py" method = "POST">
<label for="uname"><b>Username</b></label>
<input type="text" placeholder="Enter Username" name="uname" required>
<br>
<br>
<label for="psw"><b>Password</b></label>
<input type="password" placeholder="Enter Password" name="psw" required>
<br>
<br>
<br>
<button type="submit">Login</button>
details.html
<form action = "copy1.py" method = "POST">
<label for="phone num">Phone num:</label>
<input type = "text" name = "phone num"> <br />
<br >
<br >
<label for="email">Destination path :</label>
<input type = "text" name = "email" />
<br>
<br >
<br >
<div id=submit>
<input type = "submit" value = "Submit" />
login.py:
print("Content-type: text/html")
print("")
import cgi,cgitb
def func():
form = cgi.FieldStorage()
cgitb.enable()
username=form.getvalue('uname')
password = form.getvalue('psw')
userlist=[x,g,d]
passlist=[m,ldf]
if username in userlist:
if password in passlist:
redirectURL = "http://localhost/test/details.html"
print('<html>')
print(' <head>')
print(' <meta http-equiv="refresh" content="0;url='+str(redirectURL)+'" />')
print(' </head>')
print('</html>')
else:
print('<html>')
print('<div id="topcorner"> ')
print("please enter valid password")
print('</div>')
print('</html>')
else:
print('<html>')
print('<div id="topcorner"> ')
print("please enter valid username")
print('</div>')
print('</html>')
problem:
without login also I am able to open details.html,user should authenticate first before going to some other site.
Please help me on this
If your problem is not with the authentication, try to add the following lines into your script,
print('Content-Type: text/html')
print('Location: {}'.format(redirectURL))
print() # HTTP says you have to have a blank line between headers and content
print('<html>')
print(' <head>')
...

Flask: how to send values by get method in temples? [duplicate]

This question already has answers here:
Get the data received in a Flask request
(23 answers)
Closed 5 years ago.
I have a html code such as:
<form action="/labeling?id={{id}}" method="get" target="hiddenFrame">
<input type="radio" name="options" value="x" onchange="this.form.submit()"> X<br>
<input type="radio" name="options" value="y" onchange="this.form.submit()"> y<br>
</form>
and the python code that gets the id of the element:
#app.route('/labeling', methods=['GET', 'POST'])
def labeling():
value = request.form['options']
d_id = request.form['id']
but it does not sent the values of id in the GET request? why?
Your id param is not form param, try:
d_id = request.args['id']
or better use id as method param:
<form action="{{ url_for('labeling', id=id) }}" method="POST" target="hiddenFrame">
<input type="radio" name="options" value="x" onchange="this.form.submit()"> X<br>
<input type="radio" name="options" value="y" onchange="this.form.submit()"> y<br>
</form>
#app.route('/labeling/<int:d_id>', methods=['GET', 'POST'])
def labeling(d_id):
value = request.form['options']

Categories

Resources