Flutter signup/login application with Django backend #2
Tags: django, flutter, dart, heroku
Published: 2020-04-19 06:17:59

#Next steps
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. <a href="https://ohuru.tech/blog/2020/4/15/flutter-signuplogin-application-with-django-backend-1/">Flutter signup/login application with Django backend #1</a>. 
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.
# 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: <a href="https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-18-04">this tutorial</a>).
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

### Changes required

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:
```python
pip install gunicorn
```

We will be using django-heroku package to configure our setting.py file automatically. Thus, install that too:
```python
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:
```python
pip freeze > requirements.txt
```
from HOME/

Create a Procfile
```bash
nano Procfile
```
and add : 
```bash
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 <code>HOME/HOME/settings.py</code> and add the following:
```python
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
We need to create an account on <a href="https://www.heroku.com/" >heroku</a> and install <a href="https://devcenter.heroku.com/articles/heroku-cli#download-and-install">heroku-cli</a>. These instructions suffice for Ubuntu / Windows / Mac. I use Manjaro (yay as the installer) therefore just for reference, to can be installed by:
```bash
yay -S heroku-cli
```
It is the perfect time to log in to your heroku cli, run
```bash
heroku login
```
Next, we need to create an app, run
```bash
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: <a href="https://git-scm.com/book/en/v2/Getting-Started-Installing-Git">here</a>)

```bash
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:
```bash
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 <a href = "https://dev.to/amartyadev/flutter-app-authentication-with-django-backend-1-21cp" >previous post</a> for details about the endpoints, etc.

I will be publishing the next post in the series on Monday (20/04/20). Stay tuned.

Next Post: <a href="https://ohuru.tech/blog/2020/4/19/flutter-signuplogin-application-with-django-backend-3/">Flutter signup/login application with Django backend #3</a>

Previous Post:
<a href="https://ohuru.tech/blog/2020/4/15/flutter-signuplogin-application-with-django-backend-1/">Flutter signup/login application with Django backend #1</a>