Categories
Python Answers

How to use g.user global in Python Flask?

Sometimes, we want to use g.user global in Python Flask.

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

How to use g.user global in Python Flask?

To use g.user global in Python Flask, we can set g.user to the current user’s data before a request is made in an authenticated route.

For instance, we write

@app.before_request
def load_user():
    if session["user_id"]:
        user = User.query.filter_by(username=session["user_id"]).first()
    else:
        user = {"name": "Guest"}
    g.user = user

to add a load_user function that’s called before each request.

We use the @app.before_request to make load_user run before each request.

In it, we get the user’s data from the user_id key in the session object.

And then we assign that to g.user.

Now g.user should have the current user data in our views.

Conclusion

To use g.user global in Python Flask, we can set g.user to the current user’s data before a request is made in an authenticated route.

Categories
Python Answers

How to delete a record by id in Python Flask-SQLAlchemy?

Sometimes, we want to delete a record by id in Python Flask-SQLAlchemy.

In this article, we’ll look at how to delete a record by id in Python Flask-SQLAlchemy.

How to delete a record by id in Python Flask-SQLAlchemy?

To delete a record by id in Python Flask-SQLAlchemy, we can call the session delete method.

For instance, we write

session.delete(obj1)
session.delete(obj2)

session.commit()

to call session.delete to mark obj1 and obj2 to be deleted.

And then we call session.commit to commit the delete operations.

Conclusion

To delete a record by id in Python Flask-SQLAlchemy, we can call the session delete method.

Categories
Python Answers

How to serve robot.txt, sitemap.xml static files in Python Flask?

Sometimes, we want to serve robot.txt, sitemap.xml static files in Python Flask.

In this article, we’ll look at how to serve robot.txt, sitemap.xml static files in Python Flask.

How to serve robot.txt, sitemap.xml static files in Python Flask?

To serve robot.txt, sitemap.xml static files in Python Flask, we can expose a static folder in our Flask app project’s folder.

For instance, we write

from flask import Flask

app = Flask(__name__, static_folder='static', static_url_path='')

to create a Flask instance with the static_folder argument set to the 'static' folder in our Flask app project’s folder.

Then we can put robots.txt and sitemap.xml inside it.

Conclusion

To serve robot.txt, sitemap.xml static files in Python Flask, we can expose a static folder in our Flask app project’s folder.

Categories
Python Answers

How to set content type with Python Flask?

Sometimes, we want to set content type with Python Flask.

In this article, we’ll look at how to set content type with Python Flask.

How to set content type with Python Flask?

To set content type with Python Flask, we can create a Response instance with the mimetype argument.

For instance, we write

from flask import Response
@app.route('/ajax')
def ajax():
    xml = 'foo'
    return Response(xml, mimetype='text/xml')

to create the ajax view that returns a response by creating a Response instance with the mimetyp set to 'text/xml' to return an XML response with the xml string as the body.

Conclusion

To set content type with Python Flask, we can create a Response instance with the mimetype argument.

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.