Categories
Python Answers

How to get the different parts of a Python Flask request’s URL?

Sometimes, we want to get the different parts of a Python Flask request’s URL.

In this article, we’ll look at how to get the different parts of a Python Flask request’s URL.

How to get the different parts of a Python Flask request’s URL?

To get the different parts of a Python Flask request’s URL, we can use various properties of request.

For instance, if we have

http://www.example.com/myapplication

as the root URL and we make a request to

http://www.example.com/myapplication/foo/page.html?x=y

Then we can get all parts of http://www.example.com/myapplication/foo/page.html?x=y by using the following properties

  • path – /foo/page.html
  • full_path – /foo/page.html?x=y
  • script_root – myapplication
  • base_url – http://www.example.com/myapplication/foo/page.html
  • url – http://www.example.com/myapplication/foo/page.html?x=y
  • url_root – http://www.example.com/myapplication/

Conclusion

To get the different parts of a Python Flask request’s URL, we can use various properties of request.

Categories
Python Answers

How to test code that requires a Python Flask app or request context?

Sometimes, we want to test code that requires a Python Flask app or request context.

In this article, we’ll look at how to test code that requires a Python Flask app or request context.

How to test code that requires a Python Flask app or request context?

To test code that requires a Python Flask app or request context, we can call test_client to return a test version of the app.

For instance, we write

c = app.test_client()
response = c.get('/test/url')

to call test_client on the Flask app.

And then we call get to make a GET request.

We get the app context with

with app.app_context():
    # test code

And we get the request context with:

with current_app.test_request_context():
    # test code

Conclusion

To test code that requires a Python Flask app or request context, we can call test_client to return a test version of the app.

Categories
Python Answers

How to jsonify a SQLAlchemy result set in Python Flask?

Sometimes, we want to jsonify a SQLAlchemy result set in Python Flask.

In this article, we’ll look at how to jsonify a SQLAlchemy result set in Python Flask.

How to jsonify a SQLAlchemy result set in Python Flask?

To jsonify a SQLAlchemy result set in Python Flask, we create our own serialize class.

For instance, we write

from sqlalchemy.inspection import inspect

class Serializer(object):

    def serialize(self):
        return {c: getattr(self, c) for c in inspect(self).attrs.keys()}

    @staticmethod
    def serialize_list(l):
        return [m.serialize() for m in l]

to create a Serializer class that has the static serialize_list method.

Then we use it in our model class by writing

class User(db.Model, Serializer):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String)
    password = db.Column(db.String)

    # ...

    def serialize(self):
        d = Serializer.serialize(self)
        del d['password']
        return d

We call Serializer.serialize with the self User model instance to return a dict that we can return as JSON.

Conclusion

To jsonify a SQLAlchemy result set in Python Flask, we create our own serialize class.

Categories
Python Answers

How to generate Python Django models from the database?

Sometimes, we want to generate Python Django models from the database.

In this article, we’ll look at how to generate Python Django models from the database.

How to generate Python Django models from the database?

To generate Python Django models from the database, we run manage.py with the inspectdb option.

For instance, we run

python manage.py inspectdb > models.py

to make Django inspect the database the app is connected to, generate the models, and save the model class code to models.py.

Conclusion

To generate Python Django models from the database, we run manage.py with the inspectdb option.

Categories
Python Answers

How to add a multiple choice field with Python Django?

Sometimes, we want to add a multiple choice field with Python Django.

In this article, we’ll look at how to add a multiple choice field with Python Django.

How to add a multiple choice field with Python Django?

To add a multiple choice field with Python Django, we can add a ManyToManyField.

For instance, we write

class Choices(models.Model):
  description = models.CharField(max_length=300)

class Profile(models.Model):
  user = models.ForeignKey(User, blank=True, unique=True, verbose_name='user')
  choices = models.ManyToManyField(Choices)

to create the choices field in the Profile model which is a ManyToManyField.

We use Choices as the argument to allow Choices values as the values for choices.

Conclusion

To add a multiple choice field with Python Django, we can add a ManyToManyField.