2

I have already developed an application with Python Django and it's working, I am new to Python Django and now I need to deploy it on heroku servers, there are so many blogs and websites including heroku site that explains deploying a django app on heroku from scratch I haven't found any which talks about a running app

for example all of them need installing django which makes me confused,

this is the folder structure of my app:

 myapp
 |_my_app
 | |_Settingd.py
 | |_urls.py
 | |_wsgi.py
 |__webapp
    |_statics(folder) 
    |_admin.py
    |_models.py
    |_views.py

The app is connecting to mysql server locally

Question(s):

Now I am totally confused, how do I have to deploy my running app on heroku? among the steps to deploy an app on heroku provided below which ones are mandatory for me and which ones I can escape and according to my folder structure where should be the location of requirements.txt or Procfile and what should be the content of them?

https://devcenter.heroku.com/articles/getting-started-with-django

Do I have to install virtualenv? and yes where should I run this command(in which folder) I think I don't have to install django or any database api or driver for django? since they are all already installed

Siavosh
  • 2,314
  • 4
  • 26
  • 50
  • 1
    @rajasimon maybe the answer to me and other beginners is not as obvious to you so please give an informative answer if you can – Siavosh Aug 25 '14 at 08:31
  • 2
    I've downvoted, because you actually link to the page with the *exact* answers to your questions. Simply follow the instructions there. – Daniel Roseman Aug 25 '14 at 11:03
  • @DanielRoseman I think document is not clear for beginners particularly when you try to follow the page on windows machine, so let's rephrase myself if I define Procfile and requirements in root of a running Django app do I still need virtualenv? I really have no idea what it is for – Siavosh Aug 25 '14 at 12:18
  • @DanielRoseman or Django is already installed why do I need to install it again with "pip install django-toolbelt" command these things made me confused – Siavosh Aug 25 '14 at 12:21
  • I don't know how to make it any plainer. Yes, you need to follow those instructions. As those instructions clearly say, you need virtualenv. As those instructions also say, you need to install django-toolbelt, which is a different thing from plain Django as it also includes all the other things you need to serve a Django app. – Daniel Roseman Aug 25 '14 at 12:27
  • 1
    @Siavosh The document clearly says `Start a Django app inside a Virtualenv` and if you are following the tutorial from that .. `pip install django-toolbelt`. I think here you confusing why it's again installing django ? – Raja Simon Aug 25 '14 at 12:50
  • @rajasimon that makes a bit confused firstly why the app should be running inside Virtualenv? secondly by installu django=toolbelt am I forced to use postgres? as I said my databse is MySQL and don't need to change it is it possible to use MySQL by following that instruction and third which is even a more dummy question where will the main project will be located? I mean the directory structure for me is very unclear – Siavosh Aug 25 '14 at 16:12
  • 3
    @Siavosh You don't need a `virtualenv` on Heroku. Heroku only needs your project in git along with `requirements.txt` file which should list all project dependencies including Django. In other words Heroku is a `virtualenv` where you will deploy your application using git. You need `toolbelt` for connecting to Heroku using command line. For using MySQL database you need to get any Add-on like `clearDB MySQL`, once you add a addon then Heroku sets some environment variables which can be read using `dj_database_url` as mentioned in tutorial. – anuragal Aug 26 '14 at 05:46

1 Answers1

2

So the first question of yours is why the app should be running inside Virtualenv?

what's the first step? Install Django, right? Not quite. One common problem with installing packages directly to your current site-packages area is that, if you have more than one project or use Python on your machine for things other than Django, you may run into dependency issues between your applications and the installed packages. For this reason, we'll be using virtualenv to manage our Django installation. This is common, and recommended, practice among Python and Django users.

Then install and activate your virtualenv using this command...

$ virtualenv env

$ source env/bin/activate

And finally we activated the environment. Now it'll look like this

(env)rs@rajasimon-desktop:~/studio/Project$

Then i guess your second doubt what is the purpose to install django-toolbelt ?

If you are installing django-toolbelt it will install all the dependencies or package need.

it contains Django, psycopg2, gunicorn, dj-database-url, dj-static, static

Firstly Heroku natively uses postgres. Life will be easier for you if you use that locally.

If you really want to use mysql, you have two paths to follow.

1) Run mysql locally, but convert to postgres when migrating to Heroku using the mysql2psql gem, as described here: https://devcenter.heroku.com/articles/heroku-mysql

2) Use a mysql addon like https://addons.heroku.com/cleardb

However my recommendation would be to use postgres end to end, as it is baked into Heroku, and you'll be working with the default ways of using Heroku, not against the

This is my project package i am currenlty working

(env)ri@rajasimon-desktop:~/studio/project$ pip freeze
Django==1.6.5
MySQL-python==1.2.5
Pillow==2.5.3
argparse==1.2.1
django-ckeditor-updated==4.2.8
wsgiref==0.1.2

where should be the location of requirements.txt & Procfile ?

How to make requirements.txt file ?

By running below command will automatically include all packages inside txt file.

pip freeze > requirements.txt

Declare process type with Procfile

The procfile is for starting the dyno when in productioin. I always right like this..

web: gunicorn project.wsgi

So finally your project structure will look like this

myapp
 |_my_app
 | |_Settingd.py
 | |_urls.py
 | |_wsgi.py
 |__webapp
 |   |_statics(folder) 
 |   |_admin.py
 |   |_models.py
 |   |_views.py
 |__manage.py
 |__requirements.txt
 |__Procfile
Raja Simon
  • 10,126
  • 5
  • 43
  • 74