1

I have a pretty standard Django project with several apps, each with its own urls, models, forms, etc...

My issue is that whenever I make a mistake in my code - e.g. writing a wrong name for a model field in a form "fields" attribute - the error raised is always the same:

django.core.exceptions.ImproperlyConfigured: The included URLconf 'myproject.urls' does not appear to have any patterns in it. If you see valid patterns in the file then the issue is probably caused by a circular import.

I would expect the error message to change according to the error made. This not happening makes everything extremely difficult to debug.

Any ideas?

  • Which version of Django are you using? – Alasdair May 20 '19 at 14:41
  • @alasdair Django v2.2.1 – Ricardo Couto May 20 '19 at 15:48
  • 1
    Do you get the same behaviour if you install Django 2.1.X or the master branch of Django? I wonder if the new reloading code in Django 2.2 means that errors are displayed less clearly than before. See [this ticket](https://code.djangoproject.com/ticket/30329). – Alasdair May 20 '19 at 16:24
  • Tried to reproduce using Django 2.2. This specific mistakes gives me `django.core.exceptions.FieldError: Unknown field(s) (something, tags) specified for Article` not your error. So something is wrong because you should get a more specific error. I think you should show us your urls.py and any other file you're importing there (views.py and forms.py) – dirkgroten May 20 '19 at 17:24
  • Also add the full error trace. – dirkgroten May 20 '19 at 17:26
  • Another example: ```from django.forms import ModelForm from myproject.utils.models import Address class AddressCreateForm(ModelForm): model = Address``` Works. But: ```from django.forms import ModelForm from myproject.utils.models import Address class AddressCreateForm(ModelForm): class Meta: model = Address``` Throws the following error: https://gist.github.com/ricardocouto8/6745877c2435f5a1715f845942309fe1 – Ricardo Couto May 20 '19 at 17:45
  • @alasdair: "Do you get the same behaviour if you install Django 2.1.X or the master branch of Django? I wonder if the new reloading code in Django 2.2 means that errors are displayed less clearly than before. See this ticket." Just changed to Django 2.1 and the error messages are now way more clear. Thanks! Can you answer so I can mark as solved? :) – Ricardo Couto May 20 '19 at 18:11

2 Answers2

0

When you run a Django project using runserver, Django loads the URL patterns. Usually, inside your URL patterns you import your views. Which in turn import your models and forms.

So if any of these files contains a syntax error or a python error at the top level (see below), this will cause loading the URLs to fail. However, if you scroll up your trace, you should see another error, the actual error that caused the imports to fail. It will say somewhere "during handling of this exception another exception occurred".

However, python is not actually running all the code it imports. A class definition will just cause python to read the class name and its attributes (a method is also an attribute).

When python imports a class, it calls its __new__() method, not its __init__() method or any other method you define. So only errors at the top level of the class (e.g. using unknown variables in the definition of attributes) or errors inside the Meta class will be thrown.

** edit **: fields = ['some_field'] inside the Meta class is indeed run when the class object is created (__new__() method) and Django will check if the field exists in the corresponding model, because it assigns it to the class's _meta attribute.

In other cases, when you say "whenever I make a mistake in my code" that should generally not raise an error because python should not be actually instantiating the form (running its __init__() method) or running any of the methods you write. So you should avoid instantiating any classes at the top level of your code, instantiation should happen inside of functions/methods, then python won't hit these errors.

So check your views.py files to see if you are just importing form classes (from my_forms import MyForm) or if you are also instantiating a form at the top level of your file (form = MyForm(), note the parenthesis) or as an attribute of a class-based view.

dirkgroten
  • 20,112
  • 2
  • 29
  • 42
  • A `ModelForm` with `model = User` and `fields = ['invalid_field']` in the `Meta` class will raise `FieldError` when the class is defined, not when it is instantiated. – Alasdair May 20 '19 at 16:28
  • @Alasdair I know that. Didn't realise that was the mistake the OP mentioned. But that's not "whenever I make a mistake in my code". – dirkgroten May 20 '19 at 16:31
  • 1
    The "whenever I make a mistake in my code" might be too broad, but the OP has given a specific example of an invalid model form. I think your advice to check for `form = MyForm()` is a distraction, because as your update explains, an invalid form class is enough to trigger the error. – Alasdair May 20 '19 at 16:37
  • @Alasdair you're right. If that's the only mistake OP makes, this is not the right advice. – dirkgroten May 20 '19 at 16:39
  • Above ```during handling of this exception another exception occurred``` I get the following error: ```Exception in thread django-main-thread: Traceback (most recent call last): File "/home/ricardo/.local/share/virtualenvs/myproject-YwLDLjlv/lib/python3.5/site-packages/django/urls/resolvers.py", line 581, in url_patterns iter(patterns) TypeError: 'module' object is not iterable``` and then the ImproperlyConfigured error. I pointed out one example of that behaviour but it seems that the majority of the buggy code I write pops up this same error... – Ricardo Couto May 20 '19 at 16:54
  • You might want to show another example that leads to this that is not `fields =`. – dirkgroten May 20 '19 at 17:04
0

In Django 2.2, the runserver code has been updated. Unfortunately, this appears to have the side effect that some errors are not displayed as clearly as before.

I created ticket 30329 after reading this question. It appears you are facing a similar issue.

Alasdair
  • 298,606
  • 55
  • 578
  • 516