Home > Blockchain >  Failed to import a model into a view DJANGO
Failed to import a model into a view DJANGO

Time:02-22

Newbie to Django here.

I am trying to import a model into views to use it. However I am facing problems doing so.

First of all, here is the structure of my folder :

project structure

webpage folder structure

When I try to import one of my models into views with:

from .models import model_i_want

I get :

ImportError: attempted relative import with no known parent package

if I try

from webpage.models import model_i_want 

I get :

ModuleNotFoundError: No module named 'webpage'

if I try to import models like this

import .models 

I get this error

setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

My webapp is added in the settings, and everything was running smoothly while following the Django official tutorial. I know I should be reading the documentation and figure it out but I can't seem to understand all of it yet and I am still trying to get used to Django.

Thank you and have a nice day!

CodePudding user response:

Pls check the manage.py file and set the following os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'path to settings file')

  • Related