Moving django apps into subfolder and url.py error

后端 未结 4 977
梦谈多话
梦谈多话 2021-02-13 13:33

I have a question about putting django apps into \"apps\" subdirectory. I have the app called “faktura” in a project_root. I didn’t like the fact it lies there and I want to sto

4条回答
  •  感情败类
    2021-02-13 14:24

    I don't know why the previous answer got -1 aside from maybe a few redundant lines that can be corrected. Anyway, I found a slightly different method that doesn't involve adding anything to the python path.

    This is my final directory structure, I will explain in a moment:

    mysite
    ├── mysite
    │   ├── __init__.py
    │   ├── settings.py
    │   ├── urls.py
    │   └── wsgi.py
    ├── apps
    │   ├── __init__.py
    │   └── myfirstapp
    │       ├── __init__.py
    │       ├── admin.py
    │       ├── models.py
    │       ├── tests.py
    │       └── views.py
    └── manage.py
    

    No matter if you have just created your project or if you want to move your apps, create the apps subdirectory that should contain your apps. The trick is to add an __init__.py to that directory.

    mkdir apps
    touch apps/__init__.py
    

    Now you can move your existing apps into the apps subdirectory. If you would like to create a new one instead here are the commands:

    python manage.py mysecondapp
    mv mysecondapp apps/
    

    Warning: Don't be tempted to call python manage.py ./apps/mysecondapp. For some reason this deletes all other apps in that directory. I just lost a day of work this way.

    Next, you will need to fix a few imports. Your settings.py should be prefixed with apps:

    INSTALLED_APPS = (
        ...
        'apps.myfirstapp',
        'apps.mysecondapp'
    )
    

    Lastly, fix your project's urls.py to prefix apps:

    urlpatterns = patterns('', 
      url(r'^myfirstapp', include('apps.myfirstapp.urls')),
      ...
    )
    

    Depending on how you wrote them, you might also have to fix a few imports inside your app. Either just use from models import MyFirstModel or also prefix it using from apps.myfirstapp.models import MyFirstModel.

    In short, if you make your apps directory a python package (by adding __init__.py), you can use it as part of the import path. This should work regardless of the deployment method with no extra configuration.

提交回复
热议问题