将Django应用添加到INSTALLED_APPS会发生什么? [英] What happen when I add a Django app to INSTALLED_APPS?

查看:695
本文介绍了将Django应用添加到INSTALLED_APPS会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是情况。我有一个已安装两个应用程序的django项目。如果两个应用程序彼此独立安装,则它们似乎可以正常运行。

Here is the situation. I have a django project with two installed apps. Both apps appear to function properly if they are installed independently of each other.

但是,如果我在设置中都列出了这两个应用程序。第一个应用中的网址。因此,这使我相信第二个应用程序中的错误会引起问题。

However if I list both apps in the settings.INSTALLED_APPS the reverse() function seems to break for urls in the first app. So this leads me to believe that a bug in the second app is causing the problem.

如果我只是从设置中删除了app_2。再次工作。因此,当我将app_2添加到设置中时,问题就变成了什么魔术。INSTALLED_APPS?我应该在app_2的哪里查找导致此问题的代码?

If I simply remove app_2 from the settings.INSTALLED_APPS, app_1's url reverse() begins working again. So the question becomes what "Magic" is happening when I add app_2 to the settings.INSTALLED_APPS? Where should I be looking in app_2 for code causing this problem?

更新:

我已经稍微缩小了问题的范围,但是它变得陌生了。 app_2具有一个admin.py文件,该文件定义了一些自定义管理员视图。在该文件中,有一行调用反向:

reverse('init_script_view',args = ['id_content'])

I have narrowed down the problem a little, but it just gets stranger. app_2 has an admin.py file that defines a few custom admin views. In that file is a line that calls reverse:
reverse('init_script_view', args=['id_content'])

只要该行在admin.py文件中,所有对reverse()的调用都会失败,并出现NoReverseMatch异常。如果删除该行,一切似乎都可以正常工作。

As long as that line is in the admin.py file all calls to reverse() fail with a NoReverseMatch exception. If I remove that line, everything seems to work fine.

推荐答案

将应用添加到INSTALLED_APPS时,没有什么特别的事情发生,但是影响您的主要因素是,在调用 reverse()时会检查其视图。

Nothing particular happens when you add an app to INSTALLED_APPS, but the main thing that affects you is that its views are checked when you call reverse().

反向操作的方法是导入项目中的 all 视图,并查看与您指定的URL名称匹配的视图。但是,它非常脆弱,如果任何视图由于某种原因导致错误或无法导入,则 reverse 调用将失败。

The way reverse works is to import all the views in the project, and see which ones match the URL name you have given. However, it is quite fragile, and if any of the views cause an error for some reason, or can't be imported, the reverse call will fail.

只有在包含app2后它才会失败,这表明app2中某处的视图存在问题。尝试从shell分别导入它们,看看会遇到什么错误。

The fact that it is only failing once you include app2 indicates that there is an issue with the views in app2 somewhere. Try importing them individually from the shell and see what errors you get.

更新后编辑:感谢您提供额外的详细信息。我以前在自己的代码中已经看到了这一点。可能是因为管理文件是在处理urlconf之前导入的,所以这种相反的操作会产生错误。尝试将 admin.autodiscover()行向下移动到urls.py的最底部,以便它是该文件中的最后一行。

Edited after update Thanks for the extra detail. I have seen this before in my own code. It is probably because the admin files are being imported before the urlconf is processed, so this reverse gives an error. Try moving the admin.autodiscover() line down to the very bottom of urls.py, so that it is the last line in that file.

这篇关于将Django应用添加到INSTALLED_APPS会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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