This project is a lite Django Rest Framework backend for personal websites.
An article describes how this project should be used. You can find it on Clever Cloud's blog.
- you know how to use the command line interface of your operating system
- python and pip already are installed. If not follow these guidelines
- virtualenv package is installed. If not follow these guidelines
- you already are familiar with Python and Django and by extension the Django Rest Framework
- git clone the project
- go inside the new folder
$ virtualenv env
$ source env/bin/activate
(On Windows useenv\Scripts\activate
)$ pip install r requirements.txt
$ python manage.py migrate
$ python manage.py collectstatic
$ python manage.py runserver
You can try your new local application by visiting http://127.0.0.1:8000/ on your favorite browser
This app exposes all it's ressources via GET methods, and dosen't requires any credentials to do so.
For any data change (POST, PATCH, PUT, DELETE) you must provide credentials.
To create an admin user :
$ python manage.py createsuperuser --email [email protected] --username your-username
To create new entry as an authenticated user, you can use:
- httpie:
$ http -a username:password POST http://127.0.0.1:8000/entries/ key="value"
- postman: create a new POST request, below Authorization select Basic Auth, then provide your credentials. Below Body select JSON (application/json) and edit the body with your keys and value then press the SEND button on the top right
- cURL:
$ curl --header "Content-Type: application/json" \
--request POST \
-u username:password \
--data '{"key":"value"}' \
http://127.0.0.1:8000/entries/
Use the same env var names as I do in settings.py, or overrride them to use custom var names By default the ALLOWED_HOSTS value is set to everything (*), make sure this is the behaviour you want or override it in settings.py .
This tutorial will use the Clever Tools CLI.
It will allow you to create, configure and deploy your application on Clever Cloud from console.
In the following steps, we will suppose that you name (adapt to your needs) in the Clever Cloud dashboard :
- your application :
django-rest-api
- your corresponding database add-on :
django-rest-api-pg
At the root of the project :
clever login
Log in the UI and close it. Then create your Python application and PostgreSQL add-on :
clever create --type python --region par django-rest-api
clever addon create --region eu --plan dev --link django-rest-api postgresql-addon django-rest-api-pg
More information on parameters HERE.
If needed, you can set the scaling :
clever scale --min-flavor pico --max-flavor pico
clever scale --min-instances 1 --max-instances 1
More information on scaling HERE.
You need to setup some Environment variables accordingly to your settings.py
:
clever env set CUSTOM_SECRET_KEY "your_custom_secret_key"
clever env set PRODUCTION "True"
clever env set PORT "8080"
clever env set CC_PYTHON_MODULE "project.wsgi:application"
clever env set STATIC_FILES_PATH "static/"
clever env set STATIC_URL_PREFIX "/static"
clever env set PYTHON_BACKEND "uwsgi"
More information on Python environement variables HERE.
Nothing else left to do, just go on and deploy your app :
clever deploy
Note :
If deployement failed with some error like :
django.db.utils.OperationalError: could not translate host name "XXXXXXXXXX-postgresql.services.clever-cloud.com" to address: Name or service not known
It only means that you deployed too quickly after creating the PG add-on and name propagation has not yet been done. Wait a bit and try again later with clever restart
.
Your application automatically has a domain pointing to it. You can obtain it though :
clever domain
To add other domains, more information HERE.
Happy coding !