Categories
Python Answers

How to serve static files in Flask?

Sometimes, we want to serve static files in Python Flask.

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

How to serve static files in Flask?

To serve static files in Python Flask, we can use the send_from_directory function.

For instance, we write

from flask import send_from_directory

@app.route('/file/<path:path>')
def send_file(path):
    return send_from_directory('file', path)

to call send_from_directory with the directory and path of the file to return the file as the response when we go to /file/.

send_from_directory can safely handle paths for files under a known directory.

Conclusion

To serve static files in Python Flask, we can use the send_from_directory function.

Categories
Python Answers

How to get the data received in a Python Flask request?

Sometimes, we want to get the data received in a Flask request.

In this article, we’ll look at how to get the data received in a Flask request.

How to get the data received in a Flask request?

To get the data received in a Flask request, we can use various properties of request.

To get URL query parameters, we use request.args.

For instance, we write

page = request.args.get("page")

in our route function to get the page query parameter.

request.args in a dictionary.

To get form data, we use the request.form dictionary.

For instance, we write

email = request.form.get('email')

to get the form value with key email in our route function.

And to get JSON request data, we use the request.json in our route function.

Conclusion

To get the data received in a Flask request, we can use various properties of request.

To get URL query parameters, we use request.args.

Categories
Python Answers

How to rename a model and relationship fields with Python Django migrations?

Sometimes, we want to rename a model and relationship fields with Python Django migrations.

In this article, we’ll look at how to rename a model and relationship fields with Python Django migrations.

How to rename a model and relationship fields with Python Django migrations?

To rename a model and relationship fields with Python Django migrations, we can use the RenameModel and RenameField methods.

For instance, we write

class Migration(migrations.Migration):

    dependencies = [
        ('myapp', '0001_initial'), 
    ]

    operations = [
        migrations.RenameModel('Foo', 'Bar'),
        migrations.RenameField('AnotherModel', 'foo', 'bar')
    ]

to create the Migration migration class that has the operations list.

We call RenameModel to rename a model field’s name from Foo to Bar.

And we call RenameField to rename the related fields from 'foo' to 'bar'.

Then we run manage.py makemigrations to run the migration.

Conclusion

To rename a model and relationship fields with Python Django migrations, we can use the RenameModel and RenameField methods.

Categories
Python Answers

How to change the header ‘Django administration’ text with Python Django admin?

Sometimes, we want to change the header ‘Django administration’ text with Python Django admin.

In this article, we’ll look at how to change the header ‘Django administration’ text with Python Django admin.

How to change the header ‘Django administration’ text with Python Django admin?

To change the header ‘Django administration’ text with Python Django admin, we set the admin.site.site_header property.

For instance, we write

from django.contrib import admin

admin.site.site_header = 'My project'          
admin.site.index_title = 'Features area'          
admin.site.site_title = 'HTML title from adminsitration'

in urls.py to set the admin.site properties to set the site header, index title, and site title.

site_header set the site’s title.

Conclusion

To change the header ‘Django administration’ text with Python Django admin, we set the admin.site.site_header property.

Categories
Python Answers

How to register users in Python Django REST framework?

Sometimes, we want to register users in Python Django REST framework.

In this article, we’ll look at how to register users in Python Django REST framework.

How to register users in Python Django REST framework?

To register users in Python Django REST framework, we can call objects.create_user on the user model.

For instance, we write

from rest_framework import serializers
from django.contrib.auth import get_user_model # If used custom user model

UserModel = get_user_model()


class UserSerializer(serializers.ModelSerializer):

    password = serializers.CharField(write_only=True)

    def create(self, validated_data):
        user = UserModel.objects.create_user(
            username=validated_data['username'],
            password=validated_data['password'],
        )

        return user

    class Meta:
        model = UserModel
        fields = ( "id", "username", "password", )

to get the user model with get_user_model.

Then we create the UserSerializer model class that calls UserModel.objects.create_user to create a new user.

Conclusion

To register users in Python Django REST framework, we can call objects.create_user on the user model.