meeb/django-distill

NoReverseMatch exception introduced in version 2.4

Closed this issue · 4 comments

After upgrading django-distill from version 1.9 to the latest 3.0.2 admin views in my app started to crash with the following error

django.urls.exceptions.NoReverseMatch: Reverse for 'app_list' with keyword arguments '{'app_label': 'web'}' not found. 1 pattern(s) tried: ['__website_manager/(?P<app_label>auth)/$']

With a trial and error I found that versions prior to 2.3 are working normally, version 2.3 crashes the whole app on startup with:

File "/Users/ivan/.local/share/virtualenvs/adesk-website-mLhlACXX/lib/python3.8/site-packages/django_distill/renderer.py", line 40, in <module>
    nspath = ':'.join(namespaces)
TypeError: sequence item 1: expected str instance, NoneType found

and versions starting from 2.4 produce errors mentioned above.

meeb commented

Did you also upgrade Django itself? Can you paste the relevant entry from urls.py as well?

I've made a minimal reproducible example.

Note the line: from django_distill.renderer import render_to_dir.

I'm using render_to_dir to programmatically regenerate some pages from admin. Importing this function seems to break Django. I looked through the module-level code in renderer.py, but found nothing suspicious there 🤷.

meeb commented

Ah right, yeah parts of django-distill were generally really only tested imported into commands or urls.py, plus the admin area can do some pretty unusual things so I'm not totally suprised something like namespacing is broken or at least different in a way that django-distill isn't coded to handle. I'll check out your minimal example repo when I get a chance. I assume it may just be writing an if handler for whatever special case for namespaces the admin is using.

meeb commented

I'm still not entirely sure why this occurs. When you call URLResolver().url_patters the URL patterns are generated recursively and cached. For some reason this breaks in the admin if this is called in a slightly different order. Importing render_to_dir was trigging the URL patterns to be generated on import, so I can only assume the admin area expects the URL patterns to not be generated before a certain point.

Either way, I've moved the loading of the URL patterns in django_distill to when the DistillRender() object is created which seems to make the default admin area happy again.

The above patch will be in the next release.