djk2 / django-tables2-column-shifter

Simple extension for django-tables2 can dynamically show or hide columns. Using JQuery, Bootstrap 3, Bootstrap 4, Bootstrap 5 and Django >=1.9
BSD 3-Clause "New" or "Revised" License
21 stars 8 forks source link

Django-Tables2-Column-Shifter <frozen importlib._bootstrap> Error When Getting Table Class #26

Closed 2firstnames closed 1 year ago

2firstnames commented 2 years ago

Hi there I am receiving a strange error that I haven't been able to fix. I posted a question on StackOverflow but I will also post here in hopes that someone may be able to point me in the right direction.

StackOverflow Link

I am using the Django-Tables2-Column-Shifter Django library successfully in my Django application from one of my applications named app1. I am running into a bizarre error when attempting to use the same logic to use Django-Tables2-Column-Shifter from another app app2 which uses a different model. The error that I am receiving is below:

  File "/home/datavizapp/.virtualenvs/venv/lib/python3.9/site-packages/django/utils/module_loading.py", line 17, in import_string
    module = import_module(module_path)
  File "/usr/local/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 972, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 984, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'project_name'

The code posted below all works as expected. The error arises in the get_table_class code when I change the following:

tabledata_table = get_table_1_class(
    self.table_class_version
)(tabledata_queryset)

to

tabledata_table = get_table_2_class(
    self.table_class_version
)(tabledata_queryset)

Below is "views.py" inside app2


from project_name.app1.models import Table1
from project_name.app2.models import Table2

from project_name.app2.tables import get_table_1_class
from project_name.app2.tables import get_table_2_class
#from project_name.app1.tables import get_table_1_class # This also works if uncommented (original code is in app1)

from django_tables2_column_shifter.tables import (
    ColumnShiftTableBootstrap4
)

from django.contrib.auth import get_user_model
User = get_user_model()
from django.views.generic import TemplateView
from django_tables2.config import RequestConfig

class Base(object):

    container_css = "span10 offset1"
    template_name = "cryptoscreener/shifter-template.html"
    table_class_version = ColumnShiftTableBootstrap4

    def get_context_data(self, **kwargs):

        context = super(Base, self).get_context_data(**kwargs)

        # Build tables
        tabledata_queryset = Table1.objects.all()
        #tabledata_queryset = Table2.objects.all() # This works fine

        # Below is where the error is triggered. This code below works but if I change it to get_table_2_class I see the error
        tabledata_table = get_table_1_class(
            self.table_class_version
        )(tabledata_queryset)

        # Turn on sorting and pagination
        RequestConfig(self.request, paginate={'per_page': 20}).configure(tabledata_table)

        context['container_css'] = self.container_css
        context['tabledata_table'] = tabledata_table
        context['tabledata_queryset'] = tabledata_queryset

class Bootstrap5(Base, TemplateView):
    container_css = "col-xs-10 col-xs-offset-1"
    template_name = "cryptoscreener/shifter-template.html"
    table_class_version = ColumnShiftTableBootstrap4

Below is "tables.py" inside app2

from project_name.app1.models import Table1

def get_table_1_class(TableClass):

    class Table1Class(TableClass):

        class Meta:
            model = Table1

    return Table2Class

# The error appears when calling get_table_2_class from views.py in app2     
from project_name.app2.models import Table2

def get_table_2_class(TableClass):

    class Table2Class(TableClass):

        class Meta:
            model = Table2

    return Table2Class

Below is the template "shifter-template.html"


{% load static %}
{% load django_tables2 %}
{% load render_table from django_tables2 %}

<!DOCTYPE html>
{% load static %}
<html lang="{{ LANGUAGE_CODE|default:"en" }}">
    <head>
        <title>Testproject</title>
        <meta charset="utf-8">
        <meta name="description" content="Testproject for django-tables2-column-shifter">
        <meta name="author" content="Grzegorz Tężycki">

        {# CSS for bootstrap #}
        {% block bootstrap_css %}
            <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
        {% endblock %}

        {% block extra_head %}{% endblock extra_head %}

        <style>
            body {
                width : 90%;
                margin: 0 auto;
            }
        </style>
    </head>

    <body>

        {% render_table tabledata_table %}

        {% block body_ekstra %}

            {# Javascripts for bootstrap #}
            {% block bootstrap_js %}
            {% endblock %}

            {# !! --> JS for column_shifter #}
            <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js" integrity="sha512-894YE6QWD5I59HgZOGReFYm4dnWc1Qt5NtvYSaNcOP+u1T9qYdvdihz0PPSiiqn/+/3e7Jo4EaG7TubfWGUrMQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
            <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
            <script src="https://cdn.jsdelivr.net/npm/popper.js@1.14.3/dist/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
            <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.1.3/dist/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
            <script type="text/javascript" src="{% static 'django_tables2_column_shifter/js/django_tables2_column_shifter.min.js' %}"></script>

        {% endblock body_ekstra %}
    </body>
</html>

I hoping someone has a solution or a suggestion on how I may further troubleshoot this problem. Thanks for taking the time out of your day to review.

djk2 commented 2 years ago

Hi. Could you give more info about which versions of Django and others libs you use? In first moment, it looks like not supported version of some lib -> If in one project it works but in second not.