Django Heroku push failing - python

I'm running accross an error trying to push my Django project up to Heroku and I was looking to see if anyone had any insight.
! Heroku push rejected, no Cedar-supported app detected
I am guessing it is because of my folder structure in the git repo but I am not sure. My project is setup like this:
/subfolder/djangoproject/
/subfolder/requirements.txt
My Proc file content looks like this:
web: python manage.py runserver 0.0.0.0:$PORT --noreload --settings=djangoproject.settings.heroku
I have my project setting split and they work fine on my local. (In other words I having a setting directory with an init.py in it.)
I tried this:
/Procfile
and this:
/subfolder/Procfile
but neither worked.
Can this folder structure be the culprit? I was under the impression that the requirements.txt was how Heroku found where the project folder was.
Thanks
I wanted to add my solve for the first issue I had and then post the new issue I am having.
The solve I first did was to move the Procfile and requirements.txt files to the root level of the project. The second thing I did was edit the Procfile in so the path to the manage.py script was to the proper location
web: python subfolder/djangoproject/manage.py runserver 0.0.0.0:$PORT --noreload --settings=djangoproject.settings.heroku
I've hit a new error now when trying to run a syncdb. It seems to go through the process but nothing takes. In other words, if I run syncdb once and then run it a second time, it wants to set everything up all over again. Any thoughts on what I may have configures wrong to make the DB not save it's data? Should I use the setting for the DB from the dashboard or keep this line in the Django config:
import dj_database_url
DATABASES['default'] = dj_database_url.config()
Thanks

You need requirements.txt or setup.py in the root of the repo.
See https://github.com/heroku/heroku-buildpack-python/blob/master/bin/detect

Related

Deploying Flask application written in one file to Heroku

I've built a flask app and I tread to deploy it on Heroku but I got this error:
(venv) MacBook-Pro-alkhas-b-shosha:myApp joodi$ git push heroku master
error: src refspec master does not match any.
error: failed to push some refs to 'https://git.heroku.com/<app-name>.git'
I think the problem because I wrote all the application on one file.
This my app structure:
all Flask code is in flasker.py.
So when I start writing on Procfile I got confused, I didn't know what I supposed to write on it, here what I wrote
web: gunicorn myApp:app
What can I do without changing the structure?
There are a couple of issues here.
Git issue
error: src refspec master does not match any.
It looks like you haven't committed any code (Git can't find a master branch on your local system). git push operates on commits, not files.
Make sure to commit your code locally before trying to push to Heroku.
Procfile issue
Also, update your Profile to point to your flasker.py file. Assuming your Flask object is called app:
web: gunicorn flasker:app
Your error message hints to you not being on the master branch.
Try this:
git push heroku your_branch:master

Heroku looking for Procfile in wrong directory

I'm trying to work with Django on Heroku and I'm following this tutorial with its Django template https://devcenter.heroku.com/articles/django-app-configuration
https://devcenter.heroku.com/articles/deploying-python
But when i run 'heroku local web' it always look in the wrong directory. I've tried to move the project
(env) D:\Study\Workbench\heroku-testing\testing\env\codeShareApp>heroku local web
[WARN] No ENV file found
[WARN] ENOENT: no such file or directory, open 'C:\Users\William\Procfile'
[FAIL] No Procfile and no package.json file found in Current Directory - See
run_foreman.js --help
The project directory is as follows
env/
codeShareApp/
.idea/
codeShareApp/
.env
manage.py
Procfile
requirements.txt
runtime
include/
Lib/
Scripts/
tcl/
EDIT:
I tried to make a new project with directory like below, but still got the same error
codeShareApp/
codeShareApp/
env/
.env
manage.py
Procfile
requirements.txt
runtime
I encounter this problem also in a project; I solved this by Making sure that the Procfile is using the correct syntax, and that there are no extra tabs or newlines in the file.
especially whitespcae.
I dont know why, but I tried it on another machine and it works just fine. It seems that it has something to do with heroku cli 6.13 because the error only occurs after it updates from version 5.12 to 6.12
The documentation says:
The file must be placed in the root directory of your application. It will not function if placed in a subdirectory.
Try to do this and it should work. See the docs for more information.

Newrelic error in Django : Configuration has already been done

I am getting this error when i am trying to run
python manage.py runserver for my Django Project,
newrelic.api.exceptions.ConfigurationError: Configuration has already been done against differing configuration file or environment. Prior configuration file used was "/home/project/newrelic.ini" and environment "staging".'
What does it mean ?? How to approach it?
Have you just setup New Relic?
I believe this is a problem with your procfile. Which you most likely just have edited.
Your procfile should look something like this, depending on your wsgi.
web: newrelic-admin run-program gunicorn hello.wsgi --log-file -

Heroku looking for the wrong wsgi file. Django

I'm on Django, but I'm not sure if it matters. Anyway, I get an Application Error, and when I check the logs I see the error:
ImportError: No module named redlibros.wsgi
And it is fine cause the wsgi file is not name "redlibros.wsgi", I don't even know where it gets that name. The module is named
WSGI_APPLICATION = 'letrasclub.wsgi.application' # on my settings
web: gunicorn letrasclub.wsgi --log-file - # on my procfile
and on my folders it looks like this:
LetrasClub
letrasclub
wsgi.py
templates
static
...
Any idea where to find the error?
EDIT
Ok, some extra info: I have a different repo, with a different Heroku remote. I copied that repo, changed the app, created a new Heroku remote and then pushed to the new one.
So, if I write
git remote -v
heroku https://git.heroku.com/letrasclub2.git (fetch)
heroku https://git.heroku.com/letrasclub2.git (push)
origin https://github.com/Alejoss/LetrasClub2.0.git (fetch)
origin https://github.com/Alejoss/LetrasClub2.0.git (push)
Looks fine, if I go to the old app location and write the same command I get the old remote, the one that is related to the "redlibros.wsgi" app, perfect.
Now, why when I try to push the new app to the new heroku remote, I get the error that means Heroku is looking for the Old wsgi file, I changed the wsgi name, I changed the Procfile, I changed the wsgi file declaration on the settings, what am I missing?
You look at wrong project.
You look at https://github.com/Alejoss/redlibros (I guess it's Your project), not at https://github.com/Alejoss/LetrasClub2.0

How to deploy / migrate an existing django app / project to a production server on Heroku?

I have a basic django app (Newsdiffs)that runs just fine at localhost:8000 with python website/manage.py runserver but I'd like to migrate it to Heroku and I can't figure out what my next step is.
I thought getting it running locally would translate to running it on Heroku, but I'm realizing that python website/manage.py runserver is launching the dev settings and I'm not sure how to tell it to use the main settings.
All that is in my Procfile is this:
web: python website/manage.py runserver
Locally, that works fine, though it launches it at http://127.0.0.1:8000/ which is probably not what I want on Heroku. So how do I figure out where to set the hostname and port? I don't see either in the app anyplace.
I have just drawn this list for myself two days ago.
It was put together after having followed the steps described in Heroku's help pages for python.
It's by no means definitive nor perfect, and it will change, but it's a valid trace, since I was able to put the site online.
Some issues remain, to be checked thoroughly, e.g. the location of the media/ directory where files are uploaded should/could live outside your project for security reasons (now it works, but I have noticed if the dyno sleeps then the files are not reached/displayed by the template later).
The same goes for the staticfiles/ directory (although this one seems to work fine).
Also, you might want to set django's debug mode to false.
So here it is:
My first steps to deploy an EXISTING django application to Heroku
ASSUMPTIONS:
a) your django project is in a virtual environment already
b) you have already collected all your project's required packages with
pip freeze > requirements.txt
and committed it to git
git add requirements.txt
git commit -m 'my prj requirements'
0) Activate your project's virtual environment
workon xyz #using virtualenvwrapper
then go to your django project's directory (DPD for short) if not already taken there
cd ~/prj/xyz (or cdproject with virtualenvwrapper if setup properly)
and create a new git branch for heroku twiddling to prevent messing things up
git checkout -b he
1) Create the app on heroku
heroku create xyz
that also adds heroku as a remote of your repo
2) Add the needed packages to requirements.txt
vi requirements.txt
add
dj-database-url==0.3.0
django-postgrespool==0.3.0
gunicorn==19.3.0
psycopg2==2.6
django-toolbelt==0.0.1
static3==0.5.1
whitenoise==2.0.3
3) Install all dependencies in the local venv
pip install -r requirements.txt --allow-all-external
4) Setup the heroku django settings
cd xyz
create a copy
cp setting.py settings_heroku.py
and edit it
vi settings_heroku.py
import os
import dj_database_url
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'), )
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles")
STATIC_URL = '/static/'
MEDIA_URL = '/media/'
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'
SECRET_KEY = os.environ["DJANGO_SECRET_KEY"]
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
replace django's std db cfg with
DATABASES['default'] = dj_database_url.config()
DATABASES['default']['ENGINE'] = 'django_postgrespool'
and
WSGI_APPLICATION = 'xyz.wsgi_heroku.application'
5) Configure the necessary environment variables (heroku configs)
edit the .env file
vi .env
e.g.
DJANGO_SECRET_KEY=whatever
EMAIL_HOST_USER=youruser#gmail.com
EMAIL_HOST_PASSWORD=whateveritis
and/or set them manually if needed (in my case .env had no effect, wasn't loaded apparently, and had to set the vars manually for now)
heroku config:set DJANGO_SECRET_KEY=whatever
heroku config:set EMAIL_HOST_USER=youruser#gmail.com
heroku config:set EMAIL_HOST_PASSWORD=whateveritis
6) Create a separate wsgi file for heroku
cd xyx
cp wsgi.py wsgi_heroku.py
and edit it to make it point to the right settings
vi wsgi_heroku.py
from whitenoise.django import DjangoWhiteNoise
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "xyz.settings_heroku")
application = get_wsgi_application()
application = DjangoWhiteNoise(application)
7) Make sure all the templates use
{% load staticfiles %}
8) Define the Procfile file so that it points to the right wsgi
e.g.
cd ~/prj/xyz (DPD)
vi Procfile
add
web: gunicorn xyz.wsgi_heroku --log-file -
9) Collect all static content into DPD/staticfiles/
locally, make sure django points to the right wsgi settings
export WSGI_APPLICATION=blogger.wsgi_heroku.application
python manage.py collectstatic
10) add the changes to the local git repo (he branch)
git add --all .
git commit -m 'first 4 heroku'
11) check the whole thing works locally
heroku local # in heroku's help they also add `web`, not needed?!
12) push your code to heroku
git push heroku he:master
13) make sure a instance of the app is running
heroku ps:scale web=1
14) create the tables on the heroku DB
heroku run python manage.py migrate
Note: if you see a message that says, “You just installed Django’s auth system, which means you don’t have any superusers defined. Would you like to create one now?”, type no.
15) add the superuser to the heroku DB
heroku run bash
python manage.py createsuperuser
and fill in the details, as usual
16) Populate the DB with the necessary fixtures
heroku run python manage.py loaddata yourfile.json
17) Visit the website page on heroku's webserver
heroku open
or go to
https://xyz.herokuapp.com/
and the admin
https://xyz.herokuapp.com/admin/
and the DB
https://xyz.herokuapp.com/db
Useful commands:
View the app's logs
heroku logs [--tail]
List add-ons deployed
heroku addons
and use one:
heroku addons:open <add-on-name>
Run a command on heroku (the remote env, where you are deploying)
heroku run python manage.py shell
heroku run bash
Set a config var on Heroku
heroku config:set VARNAME=whatever
View the config vars that are set (including the DB's)
heroku config
View postgres DB details
heroku pg
If you know some python and have a lot of experience building web apps in other languages but don't totally understand where Heroku fits, I highly recommend Discover Flask, which patched a lot of the holes in my understanding of how these pieces all fit together.
Some of the things that I worked out:
you really do need an isolated virtual environment if you're going to deploy to Heroku, because Heroku installs Python modules from the requirements.txt file.
Gunicorn is a web server, and you definitely need to run your app under Gunicorn or it won't run on Heroku.
The "Procfile" doesn't just give the command you use to run the app locally. And Heroku requires it. So if you've got an app that was built to run on Heroku and it doesn't include a Procfile, they left something out.
You don't tell Heroku what your hostname is. When you run heroku create it should tell you what your domain name is going to be. And every time you run git push heroku master (or whatever branch you're pushing, maybe it isn't master), Heroku will (try to) restart your app.
Heroku doesn't support sqlite. You have to run your Production DB in Postgres.
This doesn't directly answer my question, but it does fill in some of the missing pieces that were making it hard for me to even ask the right question. RTFM notwithstanding. :)

Categories