将Django应用移至子文件夹和url.py错误 [英] Moving django apps into subfolder and url.py error

查看:111
本文介绍了将Django应用移至子文件夹和url.py错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对将Django应用程序放入应用程序子目录有疑问。我在project_root中有一个名为 faktura的应用程序。我不喜欢它存在于此的事实,我想将所有应用程序存储在 apps子目录下。

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 store all my apps under "apps" subdirectory.

因此,我发现我可以将python路径扩展到 apps子目录,因此在互联网上查看后,我将此字符串添加到settings.py:sys中。 path.insert(0,os.path.join(PROJECT_PATH, apps))。然后,我将应用程序添加到了INSTALLED_APPS中,例如 faktura。一切工作顺利,直到我在根目录中的urls.py中添加了url(r'^ faktura /',include('faktura.urls'))。从那时起,Django会在此处全面提示以下错误消息没有名为faktura的模块: http://dpaste.com/737380 /

So, I found out that I could extend python path to "apps" subdir, so after looking in the internet, I added this string to settings.py: sys.path.insert(0, os.path.join(PROJECT_PATH, "apps")). Then I added the app to INSTALLED_APPS like "faktura". Everything worked smooth until I added url(r'^faktura/', include('faktura.urls')) to urls.py in the root. Since that, Django throws the error message "No module named faktura" full taceback is here: http://dpaste.com/737380/

这里可能出问题了,为什么只有urls.py找不到应用程序?如果我将其添加到PATH,它找不到该应用程序吗?我花了一个早上试图找出问题所在,现在我需要您的帮助。

What can be wrong here, why only urls.py can’t find the app? And does it can’t find this app if I added it to the PATH? I spent a morning trying to figure out what’s wrong and now I need your help.

推荐答案

我不知道为什么以前的除了一些可以纠正的多余行外,答案得到-1。无论如何,我发现了一种略有不同的方法,该方法不涉及在python路径中添加任何内容。

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

如果您刚刚创建了项目,或者想移动应用程序,请创建应包含应用程序的 apps 子目录。诀窍是在该目录中添加 __ init __。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

现在,您可以将现有应用移至 apps 子目录。如果您想创建一个新的,请使用以下命令:

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/

警告:不要试图调用 python manage.py ./apps/mysecondapp 。由于某种原因,这会删除该目录中的所有其他应用程序。我只是这样失去了一天的工作。

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.

下一步,您将需要修复一些导入。您的 settings.py 应该以 apps 为前缀:

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

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

最后,将项目的 urls.py 修复为前缀 apps

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

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

取决于您如何编写它们,您可能还必须在应用程序内部修复一些导入。要么使用来自模型的导入MyFirstModel ,要么使用来自应用程序的作为前缀。myfirstapp.models导入MyFirstModel

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.

简而言之,如果您将 apps 目录设置为python软件包(通过添加 __ init __。py ),则可以将其用作导入路径的一部分。无论采用哪种部署方法而无需额外配置,此方法都应该起作用。

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.

这篇关于将Django应用移至子文件夹和url.py错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆