Friendly URL with get request form - python

i have this simple html form:
<form action="test/" method="get" accept-charset="utf-8">
<input type="text" name="first" value="" id="first">
<input type="text" name="second" value="" id="second">
<input type="text" name="third" value="" id="third">
<p><input type="submit" value="Continue →"></p>
</form>
when user click submit button, i want to get friendly URL like this:
www.site.com/test/first/second/third
how can i do this?

Where the user ends up after filling out the form and clicking "Continue" depends on what you set the action attribute in your form tag to. You've currently set it to test/.
If you want them to end up at test/<first_val>/<second_val>/<third_val>/, then you can either use some JavaScript (per pythonFoo's answer), or you can redirect in the view that test/ points to, using HttpResponseRedirect:
def test_view(request):
return HttpResponseRedirect('/test/%s/%s/%s/' % request.POST['first'],
request.POST['second'],
request.POST['third])

<input type="text" name="first" value="" id="first">
<input type="text" name="second" value="" id="second">
<input type="text" name="third" value="" id="third">
<p><input type="button" value="Continue →" onclick="submitFriendly();"></p>
<script type="text/javascript">
function submitFriendly() {
window.location.href = window.location.href + document.getElementById('first').value + '/' + document.getElementById('second').value + '/' + document.getElementById('third').value;
}
</script>
This should work. It doesn't check if all inputs are filled.

Related

pyFlask is putting my inputs in my url browser

Briefly, python Flask is the workbench of web hosting I use, and I am trying to create an input form that doesn't appear in your history.
This is my form html:
<form name="ViewWindow" action="/home/ViewWindow/ViewWindowResult/">
<input name="url" type="url" required="required" placeholder="URL Here">
<input type="submit" value="Go">
</form>
And this is the python code working with the input url:
#web_site.route('/home/ViewWindow/ViewWindowResult/', methods=('GET', 'POST'))
def ViewWindowResult():
urlboi = request.values.get('url')
response = urllibrequest.urlopen(url) # import urllib.request as urllibrequest
htmlBytes = response.read()
htmlstr = htmlBytes.decode("utf8")
return html("ViewWindowResult.html", value=htmlstr)
My goal is to get here; /home/ViewWindow/ViewWindow/ViewWindowResult/,
but I end up getting here when I input "https://www.w3schools.com/tags/"; /home/ViewWindow/ViewWindowResult/?url=https%3A%2F%2Fwww.w3schools.com%2Ftags%2F
Why does Flask put my inputs in the url string? I do not intend to do this anywhere.
Edit: You can check this out by going to https://sm--supermechm500.repl.co/home/ViewWindow/
Try specifying the form method like so:
<form name="ViewWindow" action="/home/ViewWindow/ViewWindowResult/" method="post">
<input name="url" type="url" required="required" placeholder="URL Here">
<input type="submit" value="Go">
</form>
use post method like
<form name="ViewWindow" action="/home/ViewWindow/ViewWindowResult/" method="post">
<input name="url" type="url" required="required" placeholder="URL Here">
<input type="submit" value="Go">
</form
and then you python code is
#web_site.route('/home/ViewWindow/ViewWindowResult/', methods=('GET', 'POST'))
def ViewWindowResult():
input=request.form['url']
#write your code here
return(input)
its working for me it will print the url which same you entered

Flask mail api, not getting request variable properly

I'm trying to access a request from an HTML form, and send it as a mail, but I get a mail with a value of "None",
here is my code:
#app.route("/about", methods=['GET', 'POST'])
def send_message():
name = request.form.get('name')
msg = Message(
subject='Hello ' + str(name),
sender='kristofferlocktolboll#gmail.com',
recipients=
['kristofferlocktolboll#gmail.com'],
html=render_template("about.html"))
mail.send(msg)
confirm_msg = "Your message has been sent!"
return render_template("about.html", confirm_msg=confirm_msg)
I think it might be due to the fact, that I'm casting the object into a string, but if I don't do that, I will get an error due to making a conjunction between a String and another object
EDIT:
here is my html code, I have tried both using post and get as the method, but nothing works.
<form action="/send_message" method="post">
First name: <br>
<input type="text" name="name" size="35"><br>
Last name:<br>
<input type="text" name="lastname" size="35"><br>
Email-address: <br>
<input type="email" name="email" size="35"><br>
Phone-number: <br>
<input type="text" name="phone" size="35"><br>
Enter your message: <br>
<textarea type="text" name="message" rows="7" cols="40"></textarea><br>
</form>
EDIT 2:
When ever I try to display the confirm_msg it is displayed instantly, when I enter the site.
<p>{{confirm_msg}}</p>
Firstly you must add CSRF_TOKEN for your form:
<form method="post" action="/send_message">
<input type="hidden" name="csrf_token" value="{{ csrf_token() }}" />
...
.....
</form>
Also can you tell us in which page you are trying to see <p>{{confirm_msg}}</p> ?

Login a website using python script redirects at same page

i am trying to login into a website using python script having the 'form' tag like this in the source code.
<form action="trylogin.php" method="post">
<input name="authenticity_token" type="hidden" value="dpsTlD8zutj35FVWJTIqtUZGX67qQ/vab33hpPyYuaU=" />
<input id="user_username" maxlength="20" name="username" placeholder="Username" size="20" type="text" />
<input id="user_password" name="password" placeholder="Password" size="20" type="password" />
<input class="submit themed_bg themed-dark-hover-background" name="commit" type="submit" value="Login" />
</form>
and i am trying the following python code
import requests
import lxml
import lxml.html
s = requests.session()
login = s.get('url')
login_html = lxml.html.fromstring(login.text)
hidden_inputs = login_html.xpath(r'//form//input[#type="hidden"]')
form = {x.attrib["name"]: x.attrib["value"] for x in hidden_inputs}
form['username'] ='user'
form['password'] ='pass'
form['commit'] = 'Login'
response = s.post('url', data=form)
response.ok
response.url
s.get() is working fine and response.ok also gives 'true'output but url of response is same as of previous page. it seems like it is redirecting the same page. i can't login from python. What should i do Should i use header arguement in s.post()? how to know it? and i have used
form['commit'] = 'Login'
since login is in form of input not button, is it correct?
<input class="submit themed_bg themed-dark-hover-background" name="commit" type="submit" value="Login" />

Python Mechanize change unnamed input value (known id)

as is written in topic i have to change value of some input field using mechanize but i dont have name of it only id :/ Let's stick to the point.
This is how form looks:
<form id="Login" name="Login">
<div id="login-inputs-div">
<input id="Username" type="hidden" name="username"></input>
<input id="Password" type="hidden" name="password"></input>
<input id="PopupUsername" class="input-text input-text-gray" type="text" disabled="disabled" value="admin" maxlength="32" style="width:100px;"></input>
<input id="PopupPassword" class="input-text input-text-gray " type="password" maxlength="32" value="" style="width:100px;" placeholder="hasło"></input>
<input id="bt_authenticate" class="input-btn input-btn-orange translation Translations.general.btn.authenticate translated" type="submit" value="zaloguj"></input>
</div>
<div id="logout-link-div" style="display: none;"></div>
</form>
What i have to do? Fill PopupPassword using some value and later submit it?
My approach looks like:
import mechanize
url = "http://192.168.1.1"
br = mechanize.Browser()
br.set_handle_robots(False)
br.open(url)
br.select_form(name="Login")
br.form().find_control(id="PopupPassword").__setattr__("value", "something")
res = br.submit()
content = res.read()
Im looking forward for some solution, thanks in advance.
find_control() is exactly what you need here. Just don't call br.form, it is not a function:
password_field = br.form.find_control(id="PopupPassword")
password_field.value = "something"

Post form values not showing up

This is my HTML form
<form method="post">{% csrf_token %}
<strong>Start</strong><br />
Lng: <input type="text" id="start_lng"><br />
Lat: <input type="text" id="start_lat"><br />
<strong>Destination</strong><br />
Lng: <input type="text" id="dest_lng"><br />
Lat: <input type="text" id="dest_lat"><br />
<input type="submit" value="Go" />
</form>
But when i look at my Request information the only POST values i get is the "csrfmiddlewaretoken" and not any of the start_lng, start_lat and so on.
Your HTML <input> elements do not have name attribute and therefore are not considered "successful controls" for submission as defined in the HTML spec: http://www.w3.org/TR/html401/interact/forms.html#h-17.13.2
A successful control is "valid" for submission. Every successful control has its control name paired with its current value as part of the submitted form data set. A successful control must be defined within a FORM element and must have a control name.

Categories