Categories
Python Answers

How to populate initial values on Python Django forms?

Sometimes, we want to populate initial values on Python Django forms.

In this article, we’ll look at how to populate initial values on Python Django forms.

How to populate initial values on Python Django forms?

To populate initial values on Python Django forms, we can create a form instance with the initial argument set.

For instance, we write

def view(request):
    game = Game.objects.get(id=1) # just an example
    data = {'id': game.id, 'position': game.position}
    form = UserQueueForm(initial=data)
    return render_to_response('my_template.html', {'form': form})

in the view view function.

In it, we create the UserQueueForm with the initial argument set to the data dict to set the initial value of the form fields with name id and position.

And then we call render_to_response with the template path and a dict with the form in it.

Then in my_template.html, we render it with

{{ form }}

Conclusion

To populate initial values on Python Django forms, we can create a form instance with the initial argument set.

Categories
Python Answers

How to execute raw SQL in Python Flask-SQLAlchemy app?

Sometimes, we want to execute raw SQL in Python Flask-SQLAlchemy app.

In this article, we’ll look at how to execute raw SQL in Python Flask-SQLAlchemy app.

How to execute raw SQL in Python Flask-SQLAlchemy app?

To execute raw SQL in Python Flask-SQLAlchemy app, we can call db.session.execute.

For instance, we write

result = db.session.execute('SELECT * FROM my_table WHERE my_column = :val', {'val': 5})

to call db.session.execute with a SQL string with the :val placeholder.

Then we set the value of val in the dictionary in the 2nd argument.

The selected results are returned.

Conclusion

To execute raw SQL in Python Flask-SQLAlchemy app, we can call db.session.execute.

Categories
Python Answers

How to pass data from Python Flask to JavaScript in a template?

Sometimes, we want to pass data from Python Flask to JavaScript in a template.

In this article, we’ll look at how to pass data from Python Flask to JavaScript in a template.

How to pass data from Python Flask to JavaScript in a template?

To pass data from Python Flask to JavaScript in a template, we can use the tojson filter.

For instance, we write

<html>
  <head>
    <script>
      var myGeocode = {{ geocode|tojson }};
    </script>
  </head>
  <body>
    <p>Hello World</p>
    <button onclick="console.log(myGeocode[0], myGeocode[1])" />
  </body>
</html>

to convert the geocode dictionary to JSON and assign it to myGeocode .

And then we use myGeocode as an object within our JavaScript code.

Conclusion

To pass data from Python Flask to JavaScript in a template, we can use the tojson filter.

Categories
Python Answers

How to get IP address of visitors using Flask for Python?

Sometimes, we want to get IP address of visitors using Flask for Python.

In this article, we’ll look at how to get IP address of visitors using Flask for Python.

How to get IP address of visitors using Flask for Python?

To get IP address of visitors using Flask for Python, we can use the request.remote_addr property.

For instance, we write

from flask import request
from flask import jsonify

@app.route("/get_my_ip", methods=["GET"])
def get_my_ip():
    return jsonify({'ip': request.remote_addr}), 200

to get the current client’s IP address with request.remote_addr.

Conclusion

To get IP address of visitors using Flask for Python, we can use the request.remote_addr property.

Categories
Python Answers

How to access the query string in Python Flask routes?

Sometimes, we want to access the query string in Python Flask routes.

In this article, we’ll look at how to access the query string in Python Flask routes.

How to access the query string in Python Flask routes?

To access the query string in Python Flask routes, we can use the request.args property.

For instance, we write

from flask import request

@app.route('/login')
def login():
    username = request.args.get('username')
    password = request.args.get('password')

to call request.args.get with the keys of the query parameters we want to get in our view function.

Conclusion

To access the query string in Python Flask routes, we can use the request.args property.