Django

Learn how to configure Django for use with Filebase

What is Django?

Django is a Python web framework designed for rapid, highly-scalable development and deployment of web applications. Django offers a clean, pragmatic and secure design that enables developers to create web apps quickly and efficiently. Django is free and open source.

Read below to learn how to create a basic Django web app that communicates with your Filebase account and Filebase buckets.

Prerequisites:

This guide was written using Ubuntu 20.04. Commands may vary depending on your operating system.

This guide shows a sample configuration and how to connect a Django application to Filebase. Filebase is unable to provide debugging for custom applications due to their individualized nature.

1. Install Django with the following command:

pip install django

2. Confirm the Django installation with the following command:

django-admin

This command will return a list of django core commands.

3. Create a new Django project with the following command:

django-admin startproject projectname

This command will create a Django project with the base level framework.

4. Navigate to the location of the manage.py file inside your project.

Your project’s directory will be a sub directory of where you ran the startproject command. In this example, the project is located at /home/filebase/testproject.

5. Run the following command to start the Django server:

python3 manage.py runserver

6. In a web browser, navigate to your Django server running at http://127.0.0.1:8000.

You should see the following webpage:

7. Now you need to connect the Django project to Filebase.

Start by installing django-storages with the following command:

pip install django-storages

8. Next, install the boto3 library with the following command:

pip install boto3

9. Edit the settings.py file to include “storages” under the INSTALLED_APPS:

10. Configure the settings.py file to reflect your Filebase credentials and bucket information.

Add the following lines to the settings.py file, replacing values as necessary to match your configuration:

STATIC_ROOT = ‘static’

AWS_ACCESS_KEY_ID = 'Filebase-Access-Key'
AWS_SECRET_ACCESS_KEY = 'Filebase-Secret-Key'
AWS_STORAGE_BUCKET_NAME = 'Filebase-Bucket-Name'
AWS_S3_ENDPOINT_URL = 'https://s3.filebase.com'

AWS_S3_CUSTOM_DOMAIN = ‘%s.s3.filebase.com’ % AWS_STORAGE_BUCKET_NAME

AWS_LOCATION = ‘static’

STATICFILES_DORS = [
	os.path.join(BASE_DIR, ‘randomsite/static’),
]

STATIC_URL = ‘https://%s/%s/’ % (AWS_S3_CUSTOM_DOMAIN, AWS_LOCATION)
STATICFILES_STORAGE = ‘storages.backends.s3boto3.S3Boto3Storage’

11. Run the following command to pull the static objects from your Filebase bucket:

python3 manage.py collectstatic

When prompted, enter ‘yes’. The output should resemble the following:

12. In your Filebase bucket there will be a new folder titled ‘Static’ that contains your Django project files.

From here, you can interact with the Filebase bucket from within your Django project.

If you have any questions, please join our Discord server, or send us an email at hello@filebase.com

Last updated