I'm playing around with Python and Bottle a bit . Let's say I have my Python function to generate a random number, and pass it to the template I'm going to give bottle :
@bottle.route('/')
def web_index():
val = int(100*random.random()+1) #num aleatorio de 1 a 100
return bottle.template('my_template', val)
With the template containing something like
Mi valor {{val}}
to display the generated value. As far as I know, that value is injected and "lost", that is, although I can do bottle.template('my_template', dict(val=val))
it, there is no way to recover that dictionary that I am passing as a context to the template when making GET/POST calls from that template.
If I then wanted to add two buttons, say to increment/decrement the generated value, my options would be:
Make a request to the server (HTTP request) and let my Python code handle the request. I would have to send the current value in the request
val
to modify it, since on the server side I have no more context than what is passed in the URL.Do it on the client using JavaScript
The options neither go nor come to me (although since I'm exploring bottle I would like to throw more for option A). However, if instead of having a single value I have many fields, I am forced to pass them all just to be able to "refresh" the value of one.
What is, then, the best way to proceed to implement this type of functionality (modify the value of a field in a form that can have many fields via a button) taking into account that we want to use the bottle framework ?
As I mentioned in my comment, you could use sessions so that the value persists when you increment or decrement it. For this,
bottle
he recommends making use of thebeaker
.Facility
Example
Your view or controller:
The template: