3

How can I call a python script when the button is html file is clicked? I am trying to change the language of the website by clicking the button. This is what I have done so far, but I am getting an error when I hit the button "page not found" and "The current URL, foo, didn't match any of these." What am I doing wrong

login.html

<form action="/foo" method="post">
<input type="submit" name = "first_button" value="French">
<input type="submit" name = "second_button" value="Spanish">
</form>

views.py

from app import foo

def get_language_from_client(request):   
new_lang= foo()
if new_lang=="fr":
    client_lang="fr"
else:
    client_lang = translation.get_language_from_request(request)
print "client_lang:", client_lang
if "en" in client_lang:
    return 0
elif "nl" in client_lang:
    return 1
elif "fr" in client_lang:
    return 2
elif "pl" in client_lang:
    return 3
elif "ru" in client_lang:
    return 4
else:
    print "Unknown language code:", client_lang
    return 2

app.py

from flask import Flask, render_template
app = Flask(__name__)

@app.route('/')
def index():
    return render_template('login.html')

@app.route('/foo')
def foo():
    return "fr"

if __name__ == '__main__':
    app.run()

My directory structure looks like

scriboxapp
-templates
  -login.html
-views.py
-app.py

1 Answer 1

4

I would recommend using Flask for this.

Here's an example of what you can do:

index.html:

<form action="/foo" method="POST">
    <input type="submit" value="French">
</form>

app.py:

from flask import Flask, render_template

app = Flask(__name__)

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/foo', methods=['GET', 'POST'])
def foo():
    '''execute whatever code you want when the button gets clicked here'''
    return

if __name__ == '__main__':
    app.run()

When you run your app.py file and navigate to the URL where the Flask webserver is running, which in this case is localhost, you will be presented with the index.html file.

In the above example, you can see that the button containing the value French will execute your /foo route. Your function with the proper route decorator /foo is where you can execute your desired code.

To get this working, your directory structure should look something like this:

- app
    - templates
        - index.html
    - app.py
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks a bunch for responding quickly. I am not familiar with directory structure. How can I access it?
@SamraMemon By directory structure I mean the folder where your project is. Create a folder called app, then inside a file called app.py and a folder called templates. index.html goes inside templates.
I have edited my question as a response to your answer. I dont know how to write code in comments.
@SamraMemon sorry I forgot to include that your foo route needs to accept the proper HTTP requests. You can look at the Flask docs to see where to put them in the decorator :)
Can you please give me a tiny hint? I went through the docs but nothing is working unfortunately.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.