skip to Main Content

I want to change the value of a button written in html to "records". I want to adress and change the button in python. This is in order to prevent to tap the record button twice as the request form changes and to get a visual feedback that it works.

this is what i have:

@app.route('/camera', methods=['GET', 'POST'])
def camera(): 
    if request.method == 'POST':
        if request.form['record'] == 'record here' :  # <-------- here I ask what the value of the button 
            print ("is recording")                    #           is and it works fine
            p1 = threading.Thread(target=recording)
            p1.start()
    return render_template('camera.html')

this is what i want:

@app.route('/camera', methods=['GET', 'POST'])
def camera(): 
    if request.method == 'POST':
        if request.form['record'] == 'record here' :
            print ("is recording")
            p1 = threading.Thread(target=recording)
            p1.start()
            request.form['record'] = 'records' :     # <----------------- Something like this
    return render_template('camera.html')

I am a newbie in python html flask and web development in general.

2

Answers


  1. What you are looking to do should be done through JavaScript, because the most natural way to change the label of the button is through client-side programming.

    However, if you insist on doing this from server-side code, you can start by changing the template camera.html. Let’s say that you have the following button in your template:

    <input type="submit" name="record" value="record here">
    

    This should be changed to include a variable:

    <input type="submit" name="record" value="{{ record }}">
    

    Now you can send the value of this variable using render_template:

    @app.route('/camera', methods=['GET', 'POST'])
    def camera(): 
        if request.method == 'POST':
            if request.form['record'] == 'record here' :
                print ("is recording")
                p1 = threading.Thread(target=recording)
                p1.start()
                return render_template('camera.html', record='records')
        return render_template('camera.html', record='record here')
    
    
    Login or Signup to reply.
  2. What I noticed from your code is that you want to change the value of a button in camera.html page to ‘records’.

    To do so, you should pass ‘records’ as an argument to render_html and receive it in camera.html and assign it to the button’s value.

    @app.route('/camera', methods=['GET', 'POST'])
    def camera(): 
        if request.method == 'POST':
            if request.form['record'] == 'record here' :
                print ("is recording")
                p1 = threading.Thread(target=recording)
                p1.start()
                request.form['record'] = 'records' :
        return render_template('camera.html', val='records')
    

    Now in your camera.html you have a, say button or input, with a name ‘records’:

    {% if val!=None and val!="" %}
    <input type="submit" name="records", value="{{val}}" />
    {% else %}
    <input type="submit" name="records", value="record here" />
    {% endif %}
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search