Using Django backend with django-hosts and deploying using AWS ElasticBeanstalk
What django-hosts does is allows you to use subdomains in your django website for example a seperate subdomain for the app help like help.example.com or blog.example.com. You can have as many subdomains as your apps in your django project.
Follow the steps from this repo for launching elastic beanstalk environemnt inside a VPC. After finishing those steps continue from below.
I am uinsg pipenv for my virtualenv you are free to use of your choice, active pipenv virtualenv
by running pipenv shell
.
-
Install django-hosts in your virtualenv
pip install django-hosts
-
You can directly get the sample code for a running django-hosts enabled django project from this repo Or just follow the steps below to install django-hosts in your django project.
-
Now follow the installation steps stated out here.
-
Create a hostsconf folder in your project folder containing settings.py file. Add three files in this folder namely,
- init.py
- urls.py
- views.py
-
Your files should have these pieces of code in it,
Click on the raw button and copy paste the code.
-
Now create a hosts.py file alongside the settings.py file with this piece of code.
For further explanation of what's happening inside these files you visit the official docs
-
Assuming you have your elastic beanstalk environment ready, if not follow these steps. Run
eb status
and copy paste the wholeCNAME: xxxxxxxxxxx
in yourALLOWED_HOSTS
settings. -
Create additional apps for the project by running
python manage.py startapp blog
python manage.py startapp help
Add these to theINSTALLED_APPS
-
Run
eb config
if not to change theWSGIPath
to ourboilerplate/settings.py
file. -
Add some templates and use the django-hosts template tags like the templates in this repo. To use the urls don't forget to add
PARENT_HOST
setting as explained here -
Run eb deploy and test out the site, if you have your domain available, attach it to your environment and enjoy the use of subdomains with your domain.
NOTE: I have tested this project in my local machine by editing the etc/hosts folder and
pointing my localhost to djapp.com
. You can do the same with the elastic beanstalk env url by adding the subdomains to the ALLOWED_HOSTS
appended by the subdomain for example blog.elastic_benstalk_url.com