Why doesn’t it work? Everything seems right! Common causes.

Tags: django

I just answered a question on the django-users mailinglist. Everything seemed right, but there was a strange error somewhere in the admin page for some model. In the end it was a one-letter typo in urls.py: “myproject.view.something” instead of “myproject.viewS.something”. So a missing s.

That is the one thing that’s hard in every web framework: find the spots where something can be wrong when everything seems right.

Four common spots in django in my experience:

  • Typo in urls.py (like in the case of the mailinglist question).

  • A circular import. Your models.py imports from views.py and views.py imports from models.py. You get an error like “cannot import yourproject.views”, even though yourproject/views.py is right there before your eyes :-)

  • A url higher up in the urlpatterns that matches before the url you think is matching. The url should match perfectly, but you get a strange error because the wrong view is being called.

  • You’re depending on some application in your setup.py’s “install_requires” list. The application is installed. Why doesn’t it show up? You probably forgot to add it to your INSTALLED_APPS list in your settings.py. At least, that happened to me a few times. “I can import it, why doesn’t it work?!?”

Perhaps it helps someone :-)