Arundeep Chohan
Arundeep Chohan

Reputation: 9969

Django Heroku Porting

Trying to set up Django configurations for a public url.

So I ran this first.

$ echo "web: python manage.py runserver 0.0.0.0:\$PORT" > Procfile
$ git add Procfile
$ git commit -m "Specify the command to run your project"

In Procfile:

web: python manage.py runserver 0.0.0.0:\$PORT 
release: python manage.py migrate

In Settings.py:

PORT = os.getenv("PORT", default="5000")
# SECURITY WARNING: don't run with debug turned on in production!
# DEBUG = True

DEBUG = os.environ.get('DJANGO_DEBUG', '') != 'False'

ALLOWED_HOSTS = ["url"]

In env.:

PORT=5000
SECRET_KEY=value

Was using the above commands and got

(portfolio) PS C:\Users\arund\Desktop\Code\Django\portfolio-project> heroku config:get PORT
5000
(portfolio) PS C:\Users\arund\Desktop\Code\Django\portfolio-project> heroku local
[OKAY] Loaded ENV .env File as KEY=VALUE Format
3:08:10 PM web.1 |  CommandError: "0.0.0.0:\$PORT" is not a valid port number or address:port pair.
[DONE] Killing all processes with signal  SIGINT
3:08:10 PM web.1 Exited with exit code null

I also used $PORT without the backslash and $. How would I proceed from here to make the public url working.

CommandError: "0.0.0.0:PORT" is not a valid port number or address:port pair does the same thing. 

web: python manage.py runserver 0.0.0.0:5000 will work though for local

Upvotes: 0

Views: 1125

Answers (2)

Jtoby
Jtoby

Reputation: 71

Took me about 2 hours to finally figure out what was happening that's why I'm dropping this answer for anyone it might help. If you're using a Windows OS locally (say powershell on vscode), either $PORT or \$PORT will not be parsed as environmental variables. So you might want to test out with something hardcoded like 5000. But since the Heroku OS is Linux based, it is recognized. So in short, locally, you can use

'web: python manage.py runserver 0.0.0.0:5000'

and before pushing to Heroku, change the line to

'web: python manage.py runserver 0.0.0.0:$PORT'

(without the backslash, the backslash escapes on Linux).

Upvotes: 0

Swift
Swift

Reputation: 1711

Use gunicorn to serve your web application via Heroku as described here:

https://devcenter.heroku.com/articles/python-gunicorn

Regarding your error, I think you're technically escaping the $ and so its not expanding the variable.

Try removing the \ or, for debugging purposes see if this works by hard coding the port you expect to be in $PORT and see if that works, if it does, I imagine you need to set the $PORT env variable.

Upvotes: 1

Related Questions