Categories
Python Answers

How to fix POST Error 405 Method Not Allowed with Flask Python?

Sometimes, we want to fix POST Error 405 Method Not Allowed with Flask Python.

in this article, we’ll look at how to fix POST Error 405 Method Not Allowed with Flask Python.

How to fix POST Error 405 Method Not Allowed with Flask Python?

To fix POST Error 405 Method Not Allowed with Flask Python, we should make sure the action attribute of the form is set to the URL of the view that accepts POST requests.

For instance write

@app.route('/template', methods=['GET', 'POST'])
def template():
    if request.method == 'POST':
        return "Hello"
    return render_template('index.html')

to create the template view.

Then in index.html, we write

<form action="{{ url_for('template') }}" method="post">
  ...
</form>

to add a form that has the action attribute set to the URL for the template view that we get with url_for('template').

Then when we submit the form, the template view will be run since we have 'POST' in the methods list.

Conclusion

To fix POST Error 405 Method Not Allowed with Flask Python, we should make sure the action attribute of the form is set to the URL of the view that accepts POST requests.

Categories
Python Answers

How to escape jinja2 syntax in a jinja2 template with Python Flask?

Sometimes, we want to escape jinja2 syntax in a jinja2 template with Python Flask.

In this article, we’ll look at how to escape jinja2 syntax in a jinja2 template with Python Flask.

How to escape jinja2 syntax in a jinja2 template with Python Flask?

To escape jinja2 syntax in a jinja2 template with Python Flask, we can put render the template code without interpretation by putting the code in the {% raw %} block.

For instance, we write

{% raw %}

Anything in this block is treated as raw text,
including {{ curly braces }} and
{% block %}

{% endraw %}

to treat

Anything in this block is treated as raw text,
including {{ curly braces }} and
{% block %}

as raw text.

Conclusion

To escape jinja2 syntax in a jinja2 template with Python Flask, we can put render the template code without interpretation by putting the code in the {% raw %} block.

Categories
Python Answers

How to add multiple parameters in a Python Flask app route?

Sometimes, we want to add multiple parameters in a Python Flask app route.

In this article, we’ll look at how to add multiple parameters in a Python Flask app route.

How to add multiple parameters in a Python Flask app route?

To add multiple parameters in a Python Flask app route, we can add the URL parameter placeholders into the route string.

For instance, we write

@app.route('/createcm/<summary>/<change>')
def createcm(summary=None, change=None):
    #...

to create the createcm view function by using the app.route decorator.

We call app.route with the URL string with the summary and change placeholders.

And we get the values of the parameters from the parameters in the createcm function.

Conclusion

To add multiple parameters in a Python Flask app route, we can add the URL parameter placeholders into the route string.

Categories
Python Answers

How to fix CSS problems with a Python Flask web app?

Sometimes, we want to fix CSS problems with a Python Flask web app.

In this article, we’ll look at how to fix CSS problems with a Python Flask web app.

How to fix CSS problems with a Python Flask web app?

To fix CSS problems with a Python Flask web app, we should make sure our CSS file in the static files directory.

Then we can use url_for to get the URL to the CSS file.

For instance, we can put our CSS file in project_root/static/stylesheets.

Then we can reference the CSS file with a link tag by writing

<link type="text/css" rel="stylesheet" href="{{ url_for('static', filename='stylesheets/style.css') }}" />

We set the href attribute to the CSS file URL which we get with

{{ url_for('static', filename='stylesheets/style.css') }}

Conclusion

To fix CSS problems with a Python Flask web app, we should make sure our CSS file in the static files directory.

Then we can use url_for to get the URL to the CSS file.

Categories
Python Answers

How to use MySQL in Python Flask?

Sometimes, we want to use MySQL in Python Flask.

In this article, we’ll look at how to use MySQL in Python Flask.

How to use MySQL in Python Flask?

To use MySQL in Python Flask, we use the flask-mysql package.

To install it, we run

pip install flask-mysql

Then in our app’s entry point file, we add

from flask import Flask
from flaskext.mysql import MySQL

app = Flask(__name__)
mysql = MySQL()
app.config['MYSQL_DATABASE_USER'] = 'root'
app.config['MYSQL_DATABASE_PASSWORD'] = 'root'
app.config['MYSQL_DATABASE_DB'] = 'EmpData'
app.config['MYSQL_DATABASE_HOST'] = 'localhost'
mysql.init_app(app)

to set the MySQL creadentials in the config.

We call mysql.init_app with app to incorporate flask-mysql into our Flask app.

Then we can use

conn = mysql.connect()
cursor = conn.cursor()

cursor.execute("SELECT * from User")
data = cursor.fetchone()

to connect to our database with mysql.connect.

And we get the cursor with conn.cursor.

Then we make our query with

cursor.execute("SELECT * from User")
data = cursor.fetchone()

Conclusion

To use MySQL in Python Flask, we use the flask-mysql package.