This post is in continuation of the other post that written on the same topic. If you have not read that, please go ahead and give it a read so that you can follow along with this one. Flutter signup/login application with Django backend #1.
Host your application
Since this series of posts will be a detailed step by step procedure that can be replicated to get the exact same application running, I thought it would be a good idea to include this step in the process.
When it comes to hosting Django application you have two options:
1. Host it on a VPS (you need to configure gunicorn and Nginx for it refer: this tutorial).
2. Host it on Heroku (or python anywhere etc. etc).
Well, to be honest, the second method is relatively simple and we are going to do just that as our aim here is to just see how stuff works.
Hosting your application on Heroku
Heroku provides a very easy method for hosting Django applications, the first step is to modify a few settings and add a few files in order to prepare our application for hosting.
We will be deploying using gunicorn so let us install that, run:
pip install gunicorn
We will be using django-heroku package to configure our setting.py file automatically. Thus, install that too:
pip install django-heroku
The first file we need to include is requirements.txt so that Heroku can recognize your application as a python application. Thus, activate your virtual environment and run:
pip freeze > requirements.txt
Create a Procfile
and add :
web: gunicorn HOME.wsgi --log-file -
For the admin portal to run properly, we need static files' support, for that, we need to configure STATIC_ROOT property in our settings.py file, we also need to include django-heroku settings in the file. Thus, modify your
HOME/HOME/settings.py and add the following:
import django_heroku # add in the beginning ... # denotes rest of the code in between # add these lines at end STATIC_ROOT = os.path.join(BASE_DIR, 'static/') django_heroku.settings(locals())
Pushing our code to deploy it
yay -S heroku-cli
It is the perfect time to log in to your heroku cli, run
Next, we need to create an app, run
heroku create <app_name>
Now copy all your files from the HOME/ directory to this app_name/ directory we created just now.
Now is the time to run these magic commands (we are deploying using git make sure git bash is installed refer: here)
git add . git commit -m "deploying to heroku" git push heroku master
Now we need to migrate our changes on the heroku server as well. Thus run:
heroku run python manage.py migrate
Also, create a superuser with the same credentials as your local application (for consistency).
heroku run python manage.py createsuperuser
You should now be able to call your APIs with the URL you got here. Verify the functionality using POSTMAN before proceeding. Also, refer to the previous post for details about the endpoints, etc.
Previous Post: Flutter signup/login application with Django backend #1