Dmitri-Sintsov / django-jinja-knockout

Django datatables and widgets, both AJAX and traditional. Display-only ModelForms. ModelForms / inline formsets with AJAX submit and validation. Works with Django templates.
https://django-jinja-knockout.readthedocs.org/
Other
144 stars 29 forks source link

Reduce the possibility of circular imports on django startup #12

Closed kiwnix closed 4 years ago

kiwnix commented 4 years ago

Reduce the posibility of circular imports on django startup using django-jinja-knockout, simpler than the pull request #11

Dmitri-Sintsov commented 4 years ago

from . import middleware generally should work, dynamic import is a tiny bit slower. Is there really the case when it does not work?

kiwnix commented 4 years ago

Having the import inside of the method is what solved the problem to me (looking at the stacktrace it happens from time to time on django startup around jinja2 filter initialization (loading the tags from the django jinja2 template engine), i will check if there is something on my code that trigger this and report back.

Dmitri-Sintsov commented 4 years ago

Thank you for the efforts. It's strange that it works in two different Django / Jinja based projects here and it passes the unit tests. However the difference in speed should be negligible so maybe I should apply the patch.

kiwnix commented 4 years ago

I think it would help if the difference in speed is small,

After a bit of try and error it happens when you dont import ContextMiddleware or its file before the load of Urls (for example in models.py), for example in djk-sample if you comment the "from djk_sample.middleware import ContextMiddleware" from the djk_sample/models.py and replace with dummy class (or inside djk_sample/middleware.py you comment the import "from django_jinja_knockout.middleware import ContextMiddleware as BaseContextMiddleware" and create a BaseContextMiddleware dummy class it happens.

A mini-diff that exposes the error on launch to the djk-sample app:

diff --git a/djk_sample/middleware.py b/djk_sample/middleware.py
index fa020eb..032756a 100644
--- a/djk_sample/middleware.py
+++ b/djk_sample/middleware.py
@@ -1,5 +1,7 @@
-from django_jinja_knockout.middleware import ContextMiddleware as BaseContextMiddleware
+#from django_jinja_knockout.middleware import ContextMiddleware as BaseContextMiddleware

+class BaseContextMiddleware(object):
+    pass

 class ContextMiddleware(BaseContextMiddleware):

The trace that happens when trying to run "./manage.py runserver 127.0.0.1:4000":

...
  File "/home/egarcia/Proyectos/os-libs/djk-sample/djk_sample/urls.py", line 21, in <module>
    from djk_sample.views import renderer_test, UserChangeView
  File "/home/egarcia/Proyectos/os-libs/djk-sample/djk_sample/views.py", line 4, in <module>
    from django_jinja_knockout.views import create_page_context, ModelFormActionsView
  File "/home/egarcia/Proyectos/os-libs/djk-sample/.venv/lib/python3.9/site-packages/django_jinja_knockout/views/__init__.py", line 1, in <module>
    from django_jinja_knockout.views.base import (
  File "/home/egarcia/Proyectos/os-libs/djk-sample/.venv/lib/python3.9/site-packages/django_jinja_knockout/views/base.py", line 19, in <module>
    from ..context_processors import create_page_context
  File "/home/egarcia/Proyectos/os-libs/djk-sample/.venv/lib/python3.9/site-packages/django_jinja_knockout/context_processors.py", line 12, in <module>
    from .forms import renderers as forms_renderers
  File "/home/egarcia/Proyectos/os-libs/djk-sample/.venv/lib/python3.9/site-packages/django_jinja_knockout/forms/__init__.py", line 1, in <module>
    from .base import (
  File "/home/egarcia/Proyectos/os-libs/djk-sample/.venv/lib/python3.9/site-packages/django_jinja_knockout/forms/base.py", line 12, in <module>
    from ..apps import DjkAppConfig
  File "/home/egarcia/Proyectos/os-libs/djk-sample/.venv/lib/python3.9/site-packages/django_jinja_knockout/apps.py", line 5, in <module>
    from . import middleware
  File "/home/egarcia/Proyectos/os-libs/djk-sample/.venv/lib/python3.9/site-packages/django_jinja_knockout/middleware.py", line 14, in <module>
    from .views import auth_redirect
ImportError: cannot import name 'auth_redirect' from partially initialized module 'django_jinja_knockout.views' (most likely due to a circular import) (/home/egarcia/Proyectos/os-libs/djk-sample/.venv/lib/python3.9/site-packages/django_jinja_knockout/views/__init__.py)
Dmitri-Sintsov commented 4 years ago

Please try whether c8c293193749fb6012c34a381ce12430e16718f4 helps. It works locally, no dynamic import.

kiwnix commented 4 years ago

This commit solves the issue.

Thank you very much!