Python – What happens when I add a Django application to INSTALLED_APPS?

What happens when I add a Django application to INSTALLED_APPS?… here is a solution to the problem.

What happens when I add a Django application to INSTALLED_APPS?

Here’s the situation. I have a Django project with two installed apps. If these two applications are installed independently of each other, they seem to work fine.

However, if I enter the settings. INSTALLED_APPS listing both applications, the reverse() function appears to break the URL in the first application. So this leads me to believe that a bug in the second app is causing the problem.

If I just start with settings. Remove app_2 INSTALLED_APPS and the url reverse() for app_1 will start working again. So the problem becomes when I add app_2 to settings. What “magic” happens when INSTALLED_APPS? Where should I look for the code in app_2 that is causing this issue?

Update:

I’ve narrowed down the issue, but it’s gotten weirder. app_2 has a admin.py file that defines some custom management views. There is a line in the file called reverse:
reverse('init_script_view', args=['id_content'])

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

Solution

Nothing special happens when you add an application to the INSTALLED_APPS, but the main thing that affects you is checking its View when you call reverse().

The way to reverse is to import all the Views in your project and see which ones match the URL names you provided. However, it is very fragile and if any View causes an error for some reason, or fails to import, the reverse call will fail.

The

fact that app2 fails only if it is included indicates a problem with the View somewhere in app2. Try importing them separately from the shell and see what goes wrong.

Edit after update Thank you for the extra details. I’ve seen this before in my own code. It may be because the admin file was imported before the urlconf was processed, so this reverse will report an error. Try moving the admin.autodiscover() line down to the very bottom of urls.py to make it the last line of the file.

Related Problems and Solutions