how to pass value into html form in Bottle? - python

I’m novice to python and Bottle but I’m trying to develop a simple web application which will inventory items in boxes that company receives.
Using Bottle I was able to create a form which has 2 text boxes and one ‘Save’ button. I scan box ID and it get into text box1. Then I scan item ID and it get into text box2. Then I click on Save button.
It works … but after I click on ‘Save’ the form get reloaded i.e. it open blank page and I have to move back page, delete the content from text box1 and do it again until I switch to the next Box which will start with empty box1 and box2
My request: I want that every time I click on ‘Save’ button it submitted data into my database but the form stay intact i.e. not reloaded and the content of text box1 get empty. Then I could just scan next item and so on until I complete all items.
Could please someone help me with that?
Here’s how my code look for now in Bottle template:
<form action="/accession" method="GET">
Scan Box: <input type="text" size="18" name="package"> &nbsp &nbsp &nbsp
Scan Item: <input type="text" size="13" name="sample">
<input type="submit" name="save" value="Save" >
**
I slightly changed the form and now it behaves differently i.e. when I click on "Save" it stays on the same page ( which is OK ) but it empties the content of both text boxes.
I need that only one text box be cleared but another one keep the content. How could I do it?
Thanks
**
I noticed that I could use 'value' attribute with "text" box .. like this:
Scan Box: <input type="text" value="123" name="package">
In my case the value "123" should be dynamic. I do have the value in my python script that I want to replace with "123" but I don't know how to pass it into the form.
Could someone help me?
thanks

You should use a template. Here are the docs for Bottle's built-in templating; I happen to prefer Jinja2 but you can decide which to use once you've mastered the concept.
Basically, you'll create a template file that is the html you want to return. It will include something like this:
Scan Box: <input type="text" value="{package}" name="package">
And your Bottle function (which you haven't posted, so I'm making a guess here) will look something like this:
#route('/myform')
def submit():
the_package = zzz # get the value however your application chooses
return template('form1', package=the_package) # your template file is form1.tpl
The value of the_package will automatically be substituted where {package} appears in your template file.
Please try the template examples in the Bottle documentation and let us know if you have any more questions.

Related

How to find element id by specific text and click on it?

I am trying to write a bot to book a gym/gym classes.
I have managed to get throught login page etc.. but can't figure out how to search for text in element and click on it.
HTML page source:
<input type="submit" name="ctl00$MainContent$ClassStatus$ctrl0$btnBook" value="Book" id="ctl00_MainContent_ClassStatus_ctrl0_btnBook" class="btn btn-success pull-right" data-qai-id="button-ActivityID=B7GYMSB16300321 ResourceID=30212837 Duration=60 Status=Available Date=23/06/2021 16:30:00" />
So when I ask my bot to click on it via element id - it works just fine
driver.find_element_by_id('ctl00_MainContent_ClassStatus_ctrl0_btnBook').click()
The only problem is this element id is not set in stone, it does change for specific time/day/etc...
In html source I can see:
data-qai-id="button-ActivityID=B7GYMSB16300321 ResourceID=30212837 Duration=60 Status=Available Date=23/06/2021 16:30:00"
Is there any way to look for part of that test in data-qai-id = Date=23/06/2021 16:30:00 and then click on element_id containing it??
Many thanks for your time.
No you should use the date format for this purpose :
Date=23/06/2021 16:30:00
cause it going to be different evert time and your code will never work. try the below xpath
//input[contains(#name, 'MainContent_ClassStatus_')]
or
//input[contains(#id, 'MainContent_ClassStatus_') and #value='Book' and contains(#name, '$MainContent$ClassStatus$ctr')]
See if this works:
driver.find_element_by_xpath(".//input[#value='Book']").click()

Is it possible to fetch an input tag name that change everytime?

i'm trying to fill in a form.
In this one, there is one input which it name changes everytime.
The html code
<input type="hidden" name="dr_value1" value="value2"/>
'dr' are the only thing that don't change.
Is it possible to fetch the value in this case ?

How to manage multiple HTML elements with same name using WTForms, Jinja & Python

I had defined an HTML template with Jinja, where I had defined three (3) textfields with same name. Then in the backend using the get_all method I collected all the values.
Then in order to add form validation, etc, I added WTForms library. Then I defined a Form as:
class RoleForm(BaseForm):
name = fields.TextField(_('Name'))
And in the HTML page I rendered this element three (3) times.
Now, when I submit the form and the validation fails, I re-render the template using the form as input. But instead each element has the value I had entered, all textfields have the value of the 1st textfield.
Moreover if validation is ok, I use form.name.data, which does not give me all the data from the three textfields, but only the first one.
Do you know how I can handle such situation?
Thanks in advance!
You are looking for the wtforms.fields.FieldList field enclosure:
class RoleForm(BaseForm):
name = fields.FieldList(fields.TextField(_('Name')), min_entries=3)
What you need to do is to create HTML inputelements like this for instance:
<input name="row-{{ loop.index0 }}" type="checkbox">
It will render inputs like the ones below:
<input name="row-0" type="checkbox">
<input name="row-1" type="checkbox">
Inside a loop or something like that. Then you'll be able to retrieve the Form contents:
class ListForm(Form):
row = FieldList(fields.TextField('Row'))

Can't get what I want from a for-loop in template?

I have template like this:
{% for a in As%}
<div>blah--blah--</div>
<input type="hidden" name="doSomeThingTarget" value={{a.xx}}>
<input type="hidden" name="submit" value="doSomeThing">
{% endfor%}
The loop may execute couple of times, and show some submit buttons.
In python file, I code:
target = self.request.get('doSomeThingTarget')
You know, I try to use value={{a.xx}} to hold the value in a particular loop, so, when I click one of the buttons, I can figure out what's target I need to process. BUT whichever I click, I just get the value of the first loop. What's wrong with my code? How can I implement my intention?
Thank you.
You're defining a single form, with multiple copies of a hidden input element with the same name. As a result, you only get the one copy (though if you used get_all, you'd get an array of all of them).
Instead, you should include the form start and end tags inside the loop, making each iteration its own form.

How can I match the url in html

<form action='/[0-9]+' method="POST">
<input type="submit" value="delete question" name="delete">
</form>
what above is the html template I am using for the appengine project. Besides that, i created a web request handler class to handle this request. ('/[0-9]+',QuestionViewer), it is supposed to catch any url in digits. However, turns on that after I click on the delete button above, my page is directed to some url like main/[0-9], I dont know if I can use regex in the django template, or is there a away that my QuestionViewers class can catch the url in digits? since my url associated with the html page is dynamic, like the parts after / ,like /13,are changing accordingly and I cant do that only works for page 13 but not for /14 or something like these. Hope I make it clear. any helps? Thank you a lot.
That doesn't really make sense. You want to submit your form to a regex rule? What would it match against?
No, the form needs to submit to a specific url. Right now, it's trying to submit to /[0-9]+
If I understand what you are saying, and you want to submit from a url such as /13/ to your QuestionViewer at /[0-9]+, simply submit without the action attribute or set it to "" to post to the current url.
Note that if you want to use the digit captured in your regex, you need to surround your regex in parenthesis such as '/([0-9]+)/$', QuestionViewer or use a named regexp /(?P<id>[0-9]+)/$ to pass in an argument of id equal to the matched regex to QuestionViewer.
http://docs.djangoproject.com/en/1.0/topics/http/urls/#how-django-processes-a-request
The value of the action attribute must be a valid URL.
I think what you want is to generate an actual number for the action url; a number that is the number of the question that you want to delete. For example:
<form action="/1234" method="POST">
You will need to change your code to make sure you do this.

Categories