Django not running on heroku - django

Followed the "Getting started with Django on heroku/ceadar" guide to the letter and FINALLY managed to sort out some issues on app deployment
Successfully installed django psycopg2
Cleaning up...
Discovering process types
Procfile declares types -> (none)
Compiled slug size is 8.0MB
Launching... done, v4
http://<watever>.herokuapp.com deployed to Heroku
and the guide says the webserver should be up. but heroku ps shows no processes, and obviously the page doesnt load.
tail of the heroku log:
2012-01-19T10:28:29+00:00 heroku[web.1]: State changed from created to down
2012-01-19T10:28:30+00:00 heroku[web.1]: State changed from down to created
2012-01-19T10:28:32+00:00 heroku[slugc]: Slug compilation finished
2012-01-19T10:30:32+00:00 heroku[router]: Error H99 (Platform error) -> GET <watever>
.herokuapp.com/ dyno= queue= wait= service= status=503 bytes=
(venv) C:\Python27\facebook\venv\myapp>heroku ps
Process State Command
------- ----- -------
should I be starting the webserver explicitly? wat am I doing wrong?

The Procfile I use for my django/heroku instance looks like this:
web: python hellodjango/manage.py runserver "0.0.0.0:$PORT"
As has been mentioned, if you have no Procfile and you're using the default webserver, it will essentially do the above - but try being explicit. You can try running it from the heroku console instead, to see what (if any) errors it gets:
heroku run python hellodjango/manage.py runserver

I just ran into the same problem, although I don't know if it was for the same reason or not.
While executing this command from the tutorial:
django-admin.py startproject hellodjango .
I changed the name of the app to something else (not just in this command, of course), but I omitted the '.' at the end. This caused the script to create an app inside a directory of the same name rather right in the directory I was in and that caused Heroku's server-side script to fail to detect the app as a Django app.

I ran into this problem as well. Make sure your Procfile looks correct by running...
$heroku run bash
to open a terminal with access to heroku. And then...
$cat Procfile
This will show you what is actually in your Procfile. The first line (in my local file) in mine wasn't being read for some reason. Then, I had a "}" appended to my file. I was creating my file in TextEdit on a Mac - should have been using SublimeText or another text editor. You can also call
$echo "web: gunicorn hello:app" >> Procfile
Or whatever you want your Procfile to say. This will change your Procfile to whatever you echo.

I've been struggling with Django on Heroku for some time and decided to put together a very opinionated but functioning bootstrap. I hope it can help others along the road: https://github.com/callmephilip/django-heroku-bootstrap

The key is here:
Procfile declares types -> (none)
Heroku doesn't know how to run your application. I assume python hellodjango/manage.py runserver works locally for you? If so, Heroku should be able to pick this up.
Also check that you have a requirements.txt in the repo root, and a settings.py in one of your project subdirectories.

Related

How to see full Heroku stacktrace in logs?

When I deploy my Django app to Heroku, in the console (heroku logs --tail -a george-paintings) I see an error:
2021-04-15T15:14:42.109272+00:00 app[web.1]: Error: No application module specified.
2021-04-15T15:14:42.161597+00:00 heroku[web.1]: Process exited with status 1
2021-04-15T15:14:42.290903+00:00 heroku[web.1]: State changed from starting to crashed
But I can't figure out what particular went wrong. My Procfile looks like this
web: gunicorn --pythonpath application.george_paintings.george_paintings.wsgi --log-file - --log-level debug
Where can I find more info about my error?
I suspect there's nothing more to see. A good first step would be to try running heroku local to see if you are having the same problem on your development machine. This will use your Procfile whereas if you are running via python manage.py migrate or similar your Procfile will not be used.
The error gives a good hint, though:
app[web.1]: Error: No application module specified.
Gunicorn expects an application module name as a command-line argument. I suspect you intend application.george_paintings.george_paintings.wsgi to be interpreted as your application module, but it's getting swallowed up as an argument to --pythonpath:
gunicorn --pythonpath application.george_paintings.george_paintings.wsgi #...
You shouldn't need to explicitly provide a PYTHONPATH. Try removing that argument from your Procfile entirely:
gunicorn application.george_paintings.george_paintings.wsgi --log-file - --log-level debug
If this works locally with heroku local, commit the change and redeploy.
If not, take a look at your directory structure. application.george_paintings.george_paintings.wsgi is a bit of a weird module name for a Django project. Usually you'd just need something like george_paintings.wsgi (indicating that Gunicorn should use the Python module it finds at george_paintings/wsgi.py as its entry point).

Running ./manage.py migrate during Heroku deployment

I am working on a Django app, and I would like my Database migrations to be run when deploying on Heroku.
So far we have simply put the following command in the Procfile:
python manage.py migrate
When deploying the migrations are indeed run, but they seem to be run once for each dyno (and we use several dynos). As a consequence, data migrations (as opposed to pure schema migrations) are run several times, and data is duplicated.
Running heroku run python manage.py migrate after the deployment is not satisfactory since we want the database to be in sync with the code at all times.
What is the correct way to do this in Heroku?
Thanks.
This is my Procfile and it is working exactly as you describe:
release: python manage.py migrate
web: run-program waitress-serve --port=$PORT settings.wsgi:application
See Heroku docs on defining a release process:
https://devcenter.heroku.com/articles/release-phase#defining-a-release-command
The release command is run immediately after a release is created, but before the release is deployed to the app’s dyno formation. That means it will be run after an event that creates a new release:
An app build
A pipeline promotion
A config var change
A rollback
A release via the platform API
The app dynos will not boot on a new release until the release command finishes successfully.
If the release command exits with a non-zero exit status, or if it’s shut down by the dyno manager, the release will be discarded and will not be deployed to the app’s formation.
Be aware, however, this feature is still in beta.
Update:
When you have migrations that remove models and content types, Django requires a confirmation in the console
The following content types are stale and need to be deleted:
...
Any objects related to these content types by a foreign key will also be deleted. Are you sure you want to delete these content types? If you're unsure, answer 'no'. Type 'yes' to continue, or 'no' to cancel:
The migrate command in your Procfile does not respond and the release command fails. In this scenario, remove the migrate line, push live, run the migrate command manually, then add it back for future deploys.
The migrate does automatically runs on Heroku, but for now you can safely do it once your dyno is deployed with heroku run python manage.py migrate.
If production, you can put your app in maintenance first with heroku maintenance:on --app=<app name here>
Setup your Procfile like in the docs
release: python manage.py migrate
web: gunicorn myproject.wsgi --log-file -
documented at https://devcenter.heroku.com/articles/release-phase#specifying-release-phase-tasks
You can create a file bin/post_compile which will run bash commands after the build.
Note that it is still considered experimental.
Read here for more buildpack info.
See here for an example
Alternatively, Heroku is working on a new Releases feature, which aims to simplify and solve this process. (Currently in Beta).
Good luck!

How to overcome 'Coudn't find that formation' error when adding web dynos to Heroku django app?

I'm trying to get deploy a simple django app, and have successfully pushed my git repository to Heroku. However, when I attempt to run:
heroku ps:scale web=1
I get the following error
Scaling dynos... failed
! Couldn't find that formation.
Any thoughts as to what the problem might be? The contents of the Procfile (below) are correct to the best of my knowledge.
web: gunicorn my_app_name.wsgi
To state the obvious: another way to encounter this issue is if you're working on a new app, and you try running heroku ps:scale web=1 before you've actually done a git push heroku master. There is no Procfile on the Heroku server in that case, because there aren't any files at all. :D
Ensure that your Procfile has no extension.
To create a file with no extension on Windows, you can use the command notepad Procfile. from the command line.
To add yet another reason this can happen, my Procfile contained
web:gunicorn
but it should be:
web: gunicorn
As far as I can tell from all of these answers, if you run into this problem, it is very likely related to Procfile.
for those interested, I had the same problem to add a worker. to do so you need to add this line to your procfile : worker: python worker.py
For others experiencing this same issue, make sure Procfile is not ignored in git.
Delete your Procfile. Then git status. If don't see anything mentioning Procfile, you need to find remove the entry from local or global .gitignore.
I faced a similar issue while working on windows(haven't tested on other operating systems)and this worked fine for me.
Initially, I have created a file name procfile and pushed it to heroku. But, heroku expects the Procfile declaration. It is case sensitive. Hence, we should be careful while typing the filename also.
Even after changing the name to Procfile git didnt notice changes(maybe git is case-insensitive just like windows). Hence, I had to delete the file completly and had to create a new one with Procfile as the name of the file.
I had the same problem because I missed git add and git commit the file named Procfile .
You should try to use the command git status and review whether the Procfile is included.
When pushing to Heroku you must come up with something like shown in the Picture. If not your procfile has an Error.
The Procfile looks like this for my Flask app
web: gunicorn app:app
In my php project I can use
$ heroku ps:scale web=1
at the heroku directory "php-getting-started" (https://devcenter.heroku.com/articles/getting-started-with-php#prepare-the-app).
So I'm try to do this in my original application, so I tried to do again in Heroku Repository and it's work.
(sorry about the english, hehe)
I got the same problme,
1) I also configured ProcFile
but the problem still available
So used this
Remove the existing buildpacks with heroku buildpacks:clear and add them again in the right order using the heroku buildpacks:add with the --index option, making sure that the language buildpack is the last in the list
git commit --allow-empty -m "Adjust buildpacks on Heroku"
git push heroku master
Problem solved
I had a similar problem and tried the following:
Made sure Procfile does not have any extension
Procfile is spelled exactly as 'Procfile'
At the end of the day just realized that my Procfile was in my app directory. It should be on the root/project directory.

Specifying project root when deploying to Heroku

My Problem:
When using gunicorn as my WSGI HTTP server, foreman fails to find the Django (wsgi?) application.
Application Structure:
In my Django application, I have things structured like this:
<git_repository_root>/
<django_project_root>/
<configuration_root>/
The <git_repository_root> contains the project management and deployment related things (requirements.txt, Procfile, fabfile.py, etc.)
The <django_project_root> contains my Django apps and application logic.
Finally, the <configuration_root> contains my settings.py and wsgi.py.
What I have tried:
My Procfile should look like this (according to the Heroku Docs):
web: gunicorn myapp.wsgi
When running foreman start with this project layout, I get an error:
ImportError: Import by filename is not supported.
What works:
If I move my Procfile from <git_repository_root> to <git_repository_root> it works locally. After pushing to Heroku (note: Heroku sees <git_repository_root>) I can't scale any workers / add processes. I get the following:
Scaling web dynos... failed
! No such process type web defined in Procfile.
I believe I want Procfile in my <git_repository_root> anyway though - so why isn't it working? I also tried changing the Procfile to:
web: gunicorn myapp/myapp.wsgi
but no luck. Any help would be much appreciated!
Treat the entry in your Procfile like a bash command. You can cd into your <django_project_root> and then run the server.
For example, your Procfile (which should be in your <git_repository_root>) might look something like this:
web: cd <django_project_root> && gunicorn
--env DJANGO_SETTINGS_MODULE=<configuration_root>.settings
<configuration_root>.wsgi
Move your Procfile back to <git_repository_root> and use:
web: gunicorn <django_project_root>.myapp:myapp
replacing the final "myapp" with your app's class name, presumably it is indeed "myapp".
... and read the error message: it is telling you that you can't import your worker class (app) by filename (myapp.wsgi), so of course saying dirname/myapp.wsgi won't work as well. You need a Python module:class syntax.

Heroku: My Django app is giving Application Error H14: "No Web Processes Running"

According to the Heroku site when I get error H14 "No Web Processes Running" it's because I need to scale up dynos by:
heroku ps:scale web=1
However, when I do that I get the following error:
Scaling web processes... failed
! No such type as web
Does anyone know how to fix this? I want to get my site back up!
When I run heroku ps I see nothing.
UPDATE: It's not detecting my Procfile. I don't have a Procfile explicitly and didn't use one before... is it absolutely necessary?
I ran into this recently as well, my web was working fine without any Procfile, until recently...
My fix was simply to add a Procfile as follows:
web: python manage.py runserver 0.0.0.0:$PORT --noreload
Then push to heroku.
For Heroku you need to add a Procfile.
add a Procfile on same level like your manage.py file. It should be in your root directory. Be sure that you create a Procfile not a Procfile.txt or else just Procfile
in your Procfile add:
web: gunicorn projectname.wsgi
add in your requirements.txt gunicorn
gunicorn==20.0.4
If you activate automatic deploy you can try it again.
If you can remove the app, remove it and deploy it again.