This post is in continuation of the other post that I wrote about 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.
Coming to the point. I am kind of new to flutter and these are the posts I am writing as I am developing the application. I started by calling the freshly made APIs in the previous post with flutter. The problem is that even after defining port multiple times the application did not properly communicate with the localhost:8000. So, I decided to host this application and then call them.
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:
- Host it on a VPS (you need to configure gunicorn and Nginx for it refer: this tutorial).
- 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.
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())
We need to create an account on heroku and install heroku-cli. These instructions suffice for Ubuntu / Windows / Mac. I use Manjaro (yay as the installer) therefore just for reference, to can be installed by:
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 my previous post for details about the endpoints, etc.
I will be publishing the next post in the series on Monday (20/04/20). Stay tuned.
Flutter signup/login application with Django backend #1