django - deploy project to heroku - django

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.

Related

Django Heroku : python: can't open file 'manage.py': [Errno 2] No such file or directory

I am deploying a Django website on Heroku. My project is called mysite-project which contains at its root manage.py and Procfile
I can visit the heroku website after I run git push heroku master.
And the website shows:
I am assuming I do not see anything (navbar, initial page, etc.) because I did not run migrate.
If I do:
heroku run python manage.py migrate
I get the error:
python: can't open file 'manage.py': [Errno 2] No such file or directory
Which makes no sense since I am in the right directory.
In fact:
I can run python manage.py runserver and locahost works
git ls-files manage.py --outputs--> manage.py
BUT
If I do:
heroku run bash
ls manage.py
I get:
ls: cannot access 'manage.py': No such file or directory
It seems that manage.py is in my local but not in my heroku.
Procfile
web: gunicorn mysite-project.wsgi
First use heroku logs --source app --tail to make sure that the Build succeeded.
If the build is successful, make sure you have your project at the root of the git repo.
I had the same problem because my Django project was not at the root of the git repo.
My file hierarchy was first like (assuming the name of the project prototype and it has one app named app)
├──git_repo
        ├──Procfile
        ├──requirements.txt
        ├──new_folder
                ├──prototype (folder)
                ├──app (folder)
                ├──.gitignore
                ├──manage.py
                ├──db.sqlite3
I changed the hierarchy to the following and it worked
├──git_repo
        ├──Procfile
        ├──requirements.txt
        ├──prototype (folder)
        ├──app (folder)
        ├──.gitignore
        ├──manage.py
        ├──db.sqlite3
The welcome you see when you visit the site (as well as the fact, that you can't find manage.py with ls on the server) tells you, that you haven't successfully pushed your django project to Heroku yet. Try and run git push heroku master.
It may be that the push has failed, in which case you should consult the documentation. Specifically, run
$ pip install gunicorn django-heroku
$ pip freeze > requirements.txt
...and then add
import django_heroku
django_heroku.settings(locals())
...to the bottom of settings.py. Also don't forget to set STATIC_ROOT='staticfiles' in settings.pyaswell.
That screenshot attached indicates you have just created app in heroku but not pushed your local repository code to heroku app.
git push heroku master
I was getting this error too!
I solved it by specifying the path:
python <your_project_name>/manage.py migrate
This worked for me.
In my case, it was because I was using a branch name other than master. My branch name was heroku_branch, so to make things work properly, I pushed it this way
git push heroku heroku_branch:master
It was pushed this time and worked fine.
For django + heroku
web: python manage.py runserver 0.0.0.0:\$PORT

How to deploy Django on IIS with git hooks using virtenv on a remote Windows server?

I successfully installed and setup Django (2.0.2) using Python 3.6.4 on IIS on a remote Windows Server 2012 R2 (in a VPN environment) accordingly to this instruction:
http://blog.mattwoodward.com/2016/07/running-django-application-on-windows.html
I'm using SQL Server as backend and I stored the sensitive data such as the DJANGO_SECRET_KEY, DATABASE_PASSWORD etc. as environment variables in the FastCGI Settings at the IIS (see the above link, section Configure FastCGI in IIS step 14)
At this point I asked me, how to deploy my apps and do all the necessary setps like install packages from requirements.txt, collectstatic, makemigrations etc.
With googleing I found a possible solution using git hooks, especially a post-receive hook accordingly to this post:
https://dylanwooters.wordpress.com/2015/02/21/one-command-deployments-to-iis-using-git/
So I successfully setup Bonobo Git Server on the same IIS accordingly to this instruction:
https://bonobogitserver.com/install/
My thought was to put all the required commands into this post-receive hook file as I would do in the development environment. I ended up with this file at location \inetpub\wwwroot\Bonobo.Git.Server\App_Data\Repositories\myapp-master\hooks\post-receive:
#!/bin/sh
DEPLOYDIR=/c/apps/myapp
VIRTENVDIR=/c/virtualenvs/myapp
GIT_WORK_TREE="$DEPLOYDIR" git checkout -f
cd $VIRTENVDIR
source Scripts/activate
cd $DEPLOYDIR
pip install -r requirements.txt
python manage.py collectstatic --noinput
python manage.py makemigrations
python manage.py migrate
I grant permission to the Scripts/activate file and I receive no erros during push from my development environment. But the current problem is that the command at line 6 doesn't activate the virtual environment and therefore no migrations etc. happens. Normally I would activate the virtual environment with the command line prompt using these commands:
cd C:\virtualenvs\myapp
Scripts\activate.bat
but I can't use this in the post-reveive file because it uses unix-based commands.
Can anyone help me or have a better idea how to deploy in a nicer way in a windows environment without going into the cloud?

How to runserver and see website for github project files

New to Django, I am trying to view the website with its associated functionality generated by the project files here:
https://github.com/tomwalker/django_quiz
When I use the usual: manage.py runserver on the command prompt, it says that there is no manage.py file, and there isn't.
Worryingly, I followed the instructions for installation which suggested: Run pip install -r requirements.txt
...and my computer proceeded to de-install Django 2.0? What is that about, and can anyone explain how to restore settings if I messed them up completely.
The second part of the instructions for installation asks to change something in the INSTALLED_APPS and urls.py section, but where? There is nothing in the root directory and it doesn't specify which folder/app to do this in?
I don't quite understand how to "run" (see/view) these files and see this quiz app in process on my local host. What do I need to add? Why is the manage.py file not included?
Any explanation or something to point me in the right direction would be appreciated
The github project contains only Django apps. Not whole project. You need to integrate this in your Django project. You can run it by following below steps.
Create New Django Project
Clone github repo in your project. Run following commands in your project directory.
git clone https://github.com/tomwalker/django_quiz.git
mv django_quiz/* .
rm -rf django_quiz
Add essay, true_false, quiz, multichoice in your installed apps
Install requirements with pip install -r requirements.txt
Create Migrations
Run Migrations
Add url(r'^q/', include('quiz.urls')) in your project urls.
Run server with python manage.py runserver

Django deployment on heroku not working

I created a personal portfolio website for myself using django and it also includes a blog. You can see the exact directory listing and source code in my github repository by clicking here
I have the procfile and the requirements.txt file as said in the heroku website and did the following in command prompt as directed by heroku :
$ heroku login
$ heroku git:clone -a appname
$ cd appname
$ git add .
$ git commit -am "make it better"
Then collectstatic --noinput error occurred so I did this:
heroku config:set DISABLE_COLLECTSTATIC=1
Then I again repeated the deployment process and this time the deployment was successfull. And then I opened the app but the website did not appear. Instead this appeared.
Please help me deploy this website.
You are probably developing on Windows. Heroku, like most deployment environments, uses Linux; in that environment the file system is case-sensitive. Your Templates directory should be called "templates".
Please note also you should not be committing a lot of this stuff to either Github or Heroku. In particular the venv directory at the base level, and the pycache directories inside each directory, should be excluded. You can use the git exclude functionality to ensure they are not added.
Also it is very strange that you have named your templates with ".php" extensions. There is no PHP involved here.

Deploy ready Django application on Heroku servers

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