Change path to manage.py when running Git Push Heroku Master - django

I just deployed Askbot forum to heroku successfully, but sometimes when running 'git push heroku master', the automatic collectstatic process fails (to me it looks like a random failure), prompting:
-----> Python app detected
-----> Installing dependencies with pip
Cleaning up...
-----> Preparing static assets
Collectstatic configuration error. To debug, run:
$ heroku run python ./askbot/setup_templates/manage.py collectstatic --noinput`
Well I don't really know if that's the problem, but the manage.py in .askbot/setup_templates/ contains the app's native version of the file, not the one I'm using for deployment, which is located in the app's root.
How could I make git push heroku master use the right manage.py file?

Change the path in your Procfile. Typically its something like this:
web: gunicorn hellodjango.wsgi --log-file -
Adjust it:
# from:
web: gunicorn .askbot/setup_templates/yourApp.wsgi
# to:
web: gunicorn .askbot/yourApp.wsgi
to check if the path was indeed your problem run this from the terminal:
heroku run python ./manage.py collectstatic
# or
heroku run python ./yourApp.wsgi collectstatic

Deleting or renaming Manage.py in askbot/setup_templates/ solved the problem.
Git Push Heroku Master never fails whan running collectstatic ever after.
so I believe that for some reason, probably because of sys.path configuration sometimes
Git Push Heroku Master first discovered and used ./askbot/setup_templates/manage.py instead of ./manage.py (which was the right one), and encountered an ImportError.

Related

Heroku Application Error while Deploying Django application

enter image description here
Please help me with this error
It may be due to your static files.
Try the following:
heroku config:set DISABLE_COLLECTSTATIC=1
git push heroku main
heroku run python manage.py migrate
heroku run 'bower install --config.interactive=false;grunt
prep;python
manage.py collectstatic --noinput'
heroku config:unset DISABLE_COLLECTSTATIC
heroku run python manage.py collectstatic
Hope this helps.
I can also see the console says: could not find module 'whitenoise'. Have you set your requirements.txt properly?

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

Heroku push does not upload migrations

I have developed a Django project and want to deploy it on a Heroku server. The problem is that my migrations are not uploaded after 'heroku push' command.
After I run
python ./Code/manage.py makemigrations
python ./Code/manage.py migrate
I see my migration files and database locally. But, after that, when I push them to Heroku, they seem not to be there. My website on Heroku gives the error that some tables are not available and when I run bash on the server to see my files there aren't any migration files and the migration folders in my apps only have the init.py file.
I even tried to make the migrations and migrate on the release phase. This is the Procfile:
release: bash ./release_tasks.sh
web: gunicorn --pythonpath Code Code.wsgi --log-file -
and this is release_tasks.sh:
python ./Code/manage.py makemigrations
python ./Code/manage.py migrate
Again, when I push to Heroku everything is ok and I see the correct migration messages, which shows that migration was successfully carried out. But still, there is no migration file on the server and my website gives the same error.
Here is what I don't understand:
screenshot
I have migrations locally and Git is up-to-date, but I don't have them on the server.
As with any code you create locally, you need to add it to git and then commit.
./Code/manage.py makemigrations
git add ....
git commit
git push origin heroku
You should remove the makemigrations step from the release tasks, there is no point running that on Heroku.

Django on Heroku - ProgrammingError at / relation "..." does not exist

I'm getting this error. I know you usually get this error because the databases wasn't properly migrated.
When I run heroku local web, the website works fine when I go to localhost:5000.
However after I deploy the app to heroku with git push heroku master, the error comes up.
In other words, it works in my local environment. But it does not work after deploying to heroku.
I have Heroku-Postgres installed as an add-on in heroku.
What could be causing this?
excute migrations and makemigrations in bash heroku. open the terminal in the local project folder and give the following commands:
heroku run bash
~$ ./manage.py makemigrations
~$ ./manage.py migrate
~$ exit
The following steps did it for me
Make all the necessary migrations (python manage.py makemigrations) and migrate (python manage.py migrate) locally,
push to heroku master
and finally running heroku run python manage.py migrate
Solved the issue
I experienced the same error after making a change to a model and then deploying this change to heroku.
The only way I managed to fix this problem was to do the following:
Reset the database in heroku
Delete the migrations files from the migrations folder for the broken app locally (but keep the directory and the __init__.py file)
Run python manage.py makemigrations and python manage.py migrate. This will repopulate the migrations folder with clean migration files.
Push changes to master (ensure that you do not have migrations directories in .gitignore files.
Deploy changes to heroku
Run the heroku shell heroku run bash
Run python manage.py migrate
I was able to do this because my tables did not have much data in, but I would try to avoid resetting the database if I had more data in my tables.

Django Manage.py Migrate from Google Managed VM Dockerfile - How?

I'm working on a simple implementation of Django hosted on Google's Managed VM service, backed by Google Cloud SQL. I'm able to deploy my application just fine, but when I try to issue some Django manage.py commands within the Dockerfile, I get errors.
Here's my Dockerfile:
FROM gcr.io/google_appengine/python
RUN virtualenv /venv -p python3.4
ENV VIRTUAL_ENV /venv
ENV PATH /venv/bin:$PATH
# Install dependencies.
ADD requirements.txt /app/requirements.txt
RUN pip install -r /app/requirements.txt
# Add application code.
ADD . /app
# Overwrite the settings file with the PROD variant.
ADD my_app/settings_prod.py /app/my_app/settings.py
WORKDIR /app
RUN python manage.py migrate --noinput
# Use Gunicorn to serve the application.
CMD gunicorn --pythonpath ./my_app -b :$PORT --env DJANGO_SETTINGS_MODULE=my_app.settings my_app.wsgi
# [END docker]
Pretty basic. If I exclude the RUN python manage.py migrate --noinput line, and deploy using the GCloud tool, everything works fine. If I then log onto the VM, I can issue the manage.py migrate command without issue.
However, in the interest of simplifying deployment, I'd really like to be able to issue Django manage.py commands from the Dockerfile. At present, I get the following error if the manage.py statement is included:
django.db.utils.OperationalError: (2002, "Can't connect to local MySQL server through socket '/cloudsql/my_app:us-central1:my_app_prod_00' (2)")
Seems like a simple enough error, but it has me stumped, because the connection is certainly valid. As I said, if I deploy without issuing the manage.py command, everything works fine. Django can connect to the database, and I can issue the command manually on the VM.
I wondering if the reason for my problem is that the sql proxy (cloudsql/) doesn't exist when the Dockerfile is being deployed. If so, how do I get around this?
I'm new to Docker (this being my first attempt) and newish to Django, so I'm unsure of what the correct approach is for handling a deployment of this nature. Should I instead be positioning this command elsewhere?
There are two steps involved in deploying the application.
In the first step, the Dockerfile is used to build the image, which can happen on your machine or on another machine.
In the second step, the created docker image is executed on the Managed VM.
The RUN instruction is executed when the image is being built, not when it's being run.
You should move manage.py to the CMD command, which is run when the image is being run.
CMD python manage.py migrate --noinput && gunicorn --pythonpath ./my_app -b :$PORT --env DJANGO_SETTINGS_MODULE=my_app.settings my_app.wsgi