How do you import custom classes into Django views.py?

Viewed 841

I'm trying to import a group of python classes from my mdt folder into views.py, but every time I try and run the view with the localhost, I get a ModuleNotFoundError for the Network class. I also know my FoliumMap class is not importing properly as when I remove the part of the code that returns the error for the Network class, I can't access a function from the FoliumMap class. I've seen some questions about this, and tried to do what they've said, but I still either get the same ModuleNotFoundError, or it can't find the file at all. I'm just really struggling to see what I'm missing.

Other than what I have below, I haven't changed any of the Django generated files except to get the urls & localhost working.

Here's the file structure at the moment, where I'm trying to import the FoliumMap class in FoliumMap.py file, and the Network, Segment and Node classes from the Network.py file.

Here's the error page that I keep getting.

This is all the code in views.py:

from .mdt.FoliumMap import *
from .mdt.Network import *
import pickle
from django.shortcuts import render
from django.http import HttpResponse

def show_emissions(request):  
    fol = FoliumMap()
    network = pickle.load( open( "*obj filepath*", "rb" ) )
    fol.draw_network(network, metric='emissions')
    fol.add_layers()
    folium.LayerControl(collapsed=False).add_to(fol.map)
    m=fol.get_map()._repr_html_()
    context = {'my_map': m}
    return render(request, 'mdt_webapp/index.html', context)

Here's most of my settings.py (I haven't really changed the rest from the django generated version):

...
from pathlib import Path

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent

ALLOWED_HOSTS = []

# Application definition

INSTALLED_APPS = [
    'mdt_webapp',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
...

Thanks for any help.

2 Answers

Create a __init__.py file inside mdt folder. now try

Explanation: When you create __init__.py file python consider mdt as a module, and you can import any file inside that module

here the mdt directory is inside your mdt_webapp directory. And you are trying to import the classes which are located inside the mdt directory. Just remove the . from your import or you can use full path.

from mdt_webapp.mdt.FoliumMap import *

But never do a blind import in python it is not recommended. So import your classes like this.

from mdt_webapp.mdt.FoliumMap import FoliumMap
from mdt_webapp.mdt.Network import Network, Segment, Node
Related