简体   繁体   中英

How to deal with heroku renaming my root-level app?

Heroku seems to prefer the apps deployed have a certain structure, mostly that the .git and manage.py is at root level and everything else is below that.

I have inherited a Django app I'm trying to deploy for testing purposes and I don't think I can restructure it so I was wondering if I have an alternative.

The structure I've inherited has most of the files in the root folder:

./foo: 
  __init__.py, 
  .git, 
  Procfile, 
  settings.py, 
  manage.py, 
  bar/
    models.py, etc

From within foo I can run python manage.py shell and in there from foo.bar import models works.

However, when I push this to Heroku, it puts the root in /app , so foo becomes app and from foo.bar import models no longer works.

Is there any magic settings that would allow me to indicate that app is really foo and allow me to continue without refactoring the app structure and/or all the imports?

Similar question : I think my question is similar to Heroku - Django: Had to change every mentioning of myproject into app to get my site working. How to best avoid this in the future? , except I'm asking if there's anything I can do without changing the site structure.

You can try adding a line to manage.py that modifies sys.path to make sure that foo is in your path:

import sys

PROJECT_DIR = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
if PROJECT_DIR not in sys.path:
    sys.path.insert(0, PROJECT_DIR)

Although as a side note its not really good django style to have your toplevel directory be a python module, precisely because it makes deployment more complicated (I'm not POSITIVE that the above will work on heroku). I might recommend just changing your code to import from bar directly and removing foo/__init__.py .

The easiest way would be to delete foo/__init__.py and modify your import statements to import from bar instead of from foo , eg

from foo.bar.models import * 

becomes

from bar.models import *

Alternatively you can use relative imports . So if you wanted to import bar.models in bar.views , you'd do

from .models import *

The reason this is an issue is that Django 1.4 changed folder structure for newly created projects. Before 1.4 you'd have a similar structure like you described, minus foo/__init__.py . Heroku adapted Django 1.4's project structure, which is arguably better because it encapsulates the settings within the project and makes it more portable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM