susja
susja

Reputation: 321

how to pass value into html form in Bottle?

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

Upvotes: 0

Views: 4203

Answers (1)

ron rothman
ron rothman

Reputation: 18168

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.

Upvotes: 3

Related Questions