submitting a form on click of link in cherrypy - python

Below is my cherrypy code:
class MyApp:
def link_details(self, **params):
pass
link_details.exposed=True
The below is my html code:
<form name="form1">
<input type="text" name="username"> Click here
</form>
The page gets redirected, but I am not able to access the form request parameters. I want the form to be submitted to the link_details method on the click of a link in the html page. How can I do this?

You're not submitting the form, you're just linking to a different page.
The easiest way to submit a form is by using a submit button:
<form id="form1" action="/link_details" method="post">
<input type="text" name="username" />
<input type="submit" value="Submit" />
</form>
If you want to submit the form with your <a> tag:
<form id="form1" action="/link_details" method="post">
<input type="text" name="username" />
<!-- you can omit the "return false;" part, but since we don't use a "href" attribute on the anchor it does not matter -->
<a onclick="document.getElementById('form1').submit();return false;" target="_self">Submit</a>
</form>
For more information on a form's attributes (such as action and method) please visit https://developer.mozilla.org/de/docs/Web/HTML/Element/form

Related

submit multiple forms in django using custom html form

I have custom forms in the page suprated across the page
here is my first form
<form name="profile_image" class="edit-phto" method="POST" enctype="multipart/form-data">
<i class="fa fa-camera-retro"></i>
<label class="fileContainer">
Edit Display Photo
<input name="profile_image" value="" type="file" />
</label>
</form>
for profile picture
and i have another form for cover picture
<form name="cover_image" class="edit-phto" enctype="multipart/form-data">
<i class="fa fa-camera-retro"></i>
<label class="fileContainer">
Edit Cover Photo
<input name="cover_image" type="file" />
</label>
</form>
and another form for data :)
<form method="post" id="infoform" enctype="multipart/form-data">
{% csrf_token %}
.
.
.
.
<div class="submit-btns">
<button type="button" class="mtr-btn"><span>Cancel</span></button>
<button type="submit" class="mtr-btn" name="update"><span> Update</span></button>
</div>
and i have a form for search
<div class="searched">
<form method="post" class="form-search">
<input type="text" placeholder="Search Friend">
<button data-ripple><i class="ti-search"></i></button>
</form>
</div>
i just want to submit the 2 images forms and info forms when i click update button
is there a way to do sachthing in django without using form.as_p and stuff like that
Yes you can, you can just pass request.POST to each of your forms in the view and process each one as normal. However, you should be aware of conflicting names - the best way of managing this is to use form prefixes so that Django knows which bit of data relates to which form:
https://docs.djangoproject.com/en/4.0/ref/forms/api/#prefixes-for-forms

Submit Button Not Sending Info To Flask From HTML Input Boxes

I am making a website that has a submit button and multiple input fields in different divs. I am using Flask to make the website, and I want to log what is submitted from those input fields into separate variables, to eventually be stored in a database. My problem is that whenever I press the submit button, it only works with one text box, the one closest to it in the HTML.
This is the Flask code for getting the text from the input box:
#app.route('/', methods=["GET", "POST"])
def home():
if request.method == "POST":
name = request.form.get("name")
notes = request.form.get("notes")
print(notes)
return render_template("messaging.html")
Here's the HTML for the actual inputs and submissions:
<div class="content">
<div class="page-top">
<input type="text" class="user-input-box" id="name" name="name" placeholder="Name" autocomplete="off">
</div>
<div class="user-input" id="user-input">
<input type="text" class="user-input-box" name="notes" placeholder="Notes" autocomplete="off">
</div>
<button type="submit" class="message-send">Send</button>
</div>
It only works with the notes input box, not the name.
If you can help that would be great, sorry if the code isn't very good I'm new to HTML and Flask. Thanks!
any time you use button with type "submit", must use form. and button can submit your form.
Watching some HTML tutorials won't be a bad idea if you faced a challenge in forms.
<form id="form1" action="/yourRoute" method="POST"></form>
<div class="content">
<div class="page-top">
<input type="text" class="user-input-box" id="name" name="name" placeholder="Name" autocomplete="off">
</div>
<div class="user-input" id="user-input">
<input type="text" class="user-input-box" name="notes" placeholder="Notes" autocomplete="off">
</div>
<button type="submit" class="message-send">Send</button>
</div>
</form>

Contained submit button validating other forms

I have two forms on my page, each with its own fields and submit button, but whenever I use any of them, they always check the overall page fields instead of just the form they're contained in...
The simulator.html has two forms, the first one is this:
<div class="forms">
<div class="form-1">
<form method="post" action="{{ url_for('core.simulator') }}">
<div class="container">
<div class="row g-3">
<div class="col-sm-3">
<label class="form-label"><b>Capital:</b></label>
<input type="text" class="form-control" name="capital_html" required>
</div>
<div class="col-sm-3">
<label class="form-label"><b>Price:</b></label>
<input type="text" class="form-control" name="price_html" required>
</div>
</div>
</div>
<br>
<div class="d-grid gap-2 d-md-flex justify-content-md-start">
<button id="btn" type="submit" class="btn btn-info">Launch simulation!</button>
</div>
</form>
<br>
<p>Units to purchase: <b>{{simulation_units}}</b>
</div>
</div>
And the second one is this:
<h3> Screener</h3>
<div class="forms">
<div class="form-2">
<form method="post" action="{{ url_for('core.simulator') }}">
<div class="container">
<div class="row g-3">
<div class="col-sm-3">
<label class="form-label"><b>Ticker:</b></label>
<input type="text" class="form-control" name="ticker_symbol_html" placeholder="Enter Ticker Symbol" required>
</div>
</div>
</div>
<br>
<div class="d-grid gap-2 d-md-flex justify-content-md-start">
<button id="btn" type="submit" class="btn btn-info">Launch!</button>
</div>
<p>Symbol <b>{{simulation_symbol}}.
</form>
</div>
</div>
The views.py file has the back-end code for each, the first one is this:
def capital_simulator():
if request.method == 'POST':
simulation_capital = request.form.get('capital_html', '')
simulation_price = request.form.get('price_html', '')
try:
simulation_units = math.floor(float(simulation_capital) / float(simulation_price))
except KeyError:
simulation_units == 0
return render_template('simulator.html',form1=form,capital_html=simulation_capital,price_html=simulation_price,simulation_units=simulation_units)
And the second form back-end script is this:
def screener():
if request.method == 'POST':
ticker_symbol = request.form.get('ticker_symbol_html', '')
return render_template('simulator.html',ticker_symbol=ticker_symbol_html,ticker_symbol=simulation_symbol)
I thought by specifically calling the mentioned fields and containing them into forms classes, I would avoid an overall validation, but right now I'm failing to make the submit button focus on their own forms. How can I fix this, please?
You have two forms. One is within the 'form-1' div. One is within the 'form-2' div. Each one has its own <form> and </form> tags, which means they are separate forms. The first form contains two <input> fields: one called capital_html, one called price_html, and a button called btn. The second form has one <input> field called ticker_symbol_html and a button called btn.
Let's say the user fills in the first two fields and clicks the first button. That's going to send a request to whatever the URL is in the <form> tag. In the data, it will send three things:
capital_html=xxx
price_html=yyy
btn=submit
That's it. That's all you get. You don't get any fields from the other form. If the user clicks the other form, all you will get is
ticker_symbol_html=xxx
btn=submit
The problem, as you can see, is there's no easy way for you to tell which form was submitted. If you have to use the same URL for both, the usual way to solve this is to add a hidden field that gets sent with the data, like:
<input type=hidden name="ident" text="form1">
and in the second one:
<input type=hidden name="ident" text="form2">
Now, your handler can say
if request.form.get("ident") == "form1":
# Go handle first form.
else:
# Go handle second form.

Get Form values in Python using Django framework

My question here is without using Model or forms.Form can we get form values on submit using Django request object.
Here is small example to explain problem.
HTML Code :
<form action="/login/" method="post">
{% csrf_token %}
<input type="text" id="USERNAME" class="text" value="USERNAME" >
<input type="password" id="Password" value="Password" >
<div class="submit">
<input type="submit" onclick="myFunction()" value="LOGIN">
</div>
<p>Forgot Password ?</p>
</form>
views.py Code :
def dologin(request):
print('i am in do login')
print(request.method)
print(request.POST)
for key, value in request.POST.iteritems():
print(key , value)
return render(request,'webapp/login.html')
So here my key values are empty always. I have learned and capable enough to create html forms using Model and forms:Form. But to add more style changes I need to map this forms:Form object to html defined form.
Thanks in advance.
Assign a name to each input
<form action="/login/" method="post">
{% csrf_token %}
<input type="text" id="USERNAME" class="text" name="USERNAME" value="USERNAME" >
<input type="password" id="Password" value="Password" name="Password" >
<div class="submit">
<input type="submit" onclick="myFunction()" value="LOGIN">
</div>
<p>Forgot Password ?</p>
</form>
HTML form elements always need a name attribute, otherwise the browser won't have any way of sending them to the backend. It's that attribute that is the key in the POST dict.
Note that any formatting you can do with HTML on its own you can also do with Django forms; you really should use them in most circumstances.
Please add the HTML attribute using name for every form component.

Saving results of radio button click in Flask

The relevant part of my flask code is:
#app.route("/process", methods = ["GET", "POST"] )
def process_form():
#checked = request.form.getlist('option')
checked=request.form('option')
with open('checked.txt','w') as file:
file.write("%s"%checked)
# do something with checked array
return checked
and my html looks like:
<div class="container" id='tog'>
<div class="half">
<form action="/process" method="POST">
<input type="radio" name="option" value="original" />
<h3>originak value<h3>
</div>
<div class="half">
<input type="radio" name="option" value="freq" />
<h3>Term Freq<h3>
</div>
</form>
</div>
The idea is that I want to find out which radio button was selected and store the information is a text file. But that does not seem to be working.
I changed the html to:
<form action="/process" method="POST">
<div class="half">
<input type="radio" name="option" value="original" />
</div>
<div class="half">
<input type="radio" name="option" value="freq" />
</div>
<input type="submit" value="Submit">
</form>
and now I get a 400 Bad Request error
You don't have a submit button inside of your form. Add one.
<input type="submit" value="Submit!" />
Also, you probably want to change request.form('option') to request.form['option']

Categories