Deploy ready Django application on Heroku servers - django

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

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

Related

Heroku app crashes after pushing change with new dependencies

So I have a Django web app running on Heroku. I recently pushed an update where I added some API functionality via the Django Rest Framework. I installed this via pip. Now my Heroku app crashes. I assume I somehow need to install this dependency on Heroku? Here is the error message:
An error occurred in the application and your page could not be served. If you are the application owner, check your logs for details. You can do this from the Heroku CLI with the command
heroku logs --tail
I tried entering the Heroku CLI and typing pip install djangorestframework but and running Heroku restart but it still crashes.
Edit: some more details, I tried installing this dependency on my machine via git clone in addition to pip. When I pushed this code to Heroku, in the command line I see cannot stat '/tmp/build_5917e4123a7c/requirements.txt': No such file or directory
Edit2: Just to keep this post updated, I'm not trying to add a requirements.txt file to my project with this dependency in it. The file is in the root directory and the only text in it is:
djangorestframework==3.11.0
Edit3: I fixed it. See my answer
So I installed my dependency with pip install djangorestframework but I needed to use pipenv install djangorestframework. The Pipfile therefore never had this dependency added so Heroku wasn't able to see it since it seems Heroku checks the Pipfile. I guess requirements.txt is the old deprecated way to do this?

confusion in deploying module's with django

Good day.
I'm a newbie to Django and I have a slight confusion:
When deploying my Django app, do I need to deploy it with all the Python 'come-with' modules, or the hosts already have them installed.
Also, I installed PIL for image manipulation. Would they also have it installed or i have to find a way to install it on their servers. Thanks in advance
do I need to deploy it with all the Python 'come-with' modules
Never do that. It might conflict with the dependencies on the server. Instead issue the following command to create a dependency file (requirements.txt).
pip freeze > requirements.txt (issue this command where manage.py is located)
On the server create a new virtual environment. Now copy django project to the server (you can do this using git clone or just plain old Filezilla). Activate virtual environment. Then change you current working directory to the where manage.py is located. to install all the dependencies issue the following command.
pip install -r requirements.txt
This will install the required dependencies on on server.

Is it mandatory to activate virtual environment before deploying django apps on heroku?

I am trying to deploy a django website project with 3 apps on heroku. In the heroku website it is mentioned to activate virtual environment before deploying apps. But the venv file is taking a lot of space (actually it is taking the 50% of the space of my project) and deploying it is taking a lot of time. I hope for a nice answer. :)
Whether you use a virtualenv in your local development is up to you, but in either case you'll have to specify a requirements.txt file so Heroku knows what Python packages/dependencies to install.
Creating a minimal requirements.txt is very easy using a virtualenv, as outlined in Heroku's tutorial for Django.
pip freeze > requirements.txt

django - deploy project to heroku

I have an django project (RESTful API written using Django Rest Framework) which uses the Postgres database.
I have a local git repository of the project and also I have it on my github account, and I want to deploy the porject to heroku.
In the official heroku tutorials, they don't show anything about how to prepare your app to deployment - (requirements file, settings file, Proc File, maybe more stuff that I don't know - which I saw in different tutorials that you need to do).
At the moment I only have the django app without any added file.
My question is - what do I need to do to prepare my app to deployment to heroku? as I said at the moment I have a local git repository and its also on Github.
Thanks!
1) Create a file called Procfile (no extension) in your project root with the following inside:
web: gunicorn APP_NAME.wsgi (replace APP_NAME with your app's name).
2) Pip install gunicorn and dj-database-url
3) In your virtual env terminal, run pip freeze > requirements.txt in your project root (do this every time you pip install any new packages).
4) In your production settings file, add the following to make your database work on heroku:
import dj_database_url
DATABASES['default'] = dj_database_url.config()
Note: This will cause errors in your local environment, so make sure you have a prod.py settings file as well (ask if you need an explanation).
5) Add heroku to your git settings via git remote add heroku git#heroku.com:HEROKU_APP_NAME.git (replace HEROKU_APP_NAME with your Heroku app name).
6) Once you do git add --all, git commit -m "SOME MESSAGE HERE" and git push, you can do git push heroku master.

Django Buildout to Virtualenv

beginner Django developer here.
I started a personale project with buildout and now I wanted to test some deploying, I decided to go with heroku, but I immediately noticed that heroku works with virtualenv and a requirements.txt file. My question is, is there a way to deploy a buildout project to heroku or convert said project to use virtualenv? If yes, how can I achieve this?
Thanks
Buildout and virtualenv can work just fine together.
Upload your buildout.cfg, your bootstrap.py and use the virtualenv python to run the bootstrap.py script.
Kenneth Reitz (of requests fame, and Heroku's Python guy) has created a buildpack that does just that.