0

I am trying to import data from csv into the django database.

models.py

class contactDetails(models.Model):
firstname = models.CharField(max_length=100)
lastname = models.CharField(max_length=100)
phonenumber = models.IntegerField()
email = models.CharField(max_length=100)

 def __str__(self):
    return self.firstname

save_form_data is project name and AppFormSaving is an app.

import.py

project_dir = 'K:\\PY\\save_form_data'
## project_dir = 'K:\\PY\\save_form_data\AppFormSaving'
sys.path.append(project_dir)

os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'

import django

django.setup()

from AppFormSaving.models import contactDetails

data = csv.reader(open('K:\\sample.csv'), delimiter=",")

for row in data:
    if row[0] != 'firstname':
        details = contactDetails()
        details.firstname = row[0]
        details.lastname = row[1]
        details.phonenumber = row[2]
        details.email = row[3]

        details.save()

when I run import.py

    K:\PY\save_form_data>python import.py

I get an error below,

 File "C:\Python34\lib\site-packages\django\conf\__init__.py", line 55, in __geta
ttr__
    self._setup(name)
  File "C:\Python34\lib\site-packages\django\conf\__init__.py", line 43, in _setup

    self._wrapped = Settings(settings_module)
  File "C:\Python34\lib\site-packages\django\conf\__init__.py", line 99, in __init
__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "C:\Python34\lib\importlib\__init__.py", line 109, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
  File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
  File "<frozen importlib._bootstrap>", line 2224, in _find_and_load_unlocked

ImportError: No module named 'settings'

I followed this tutorial. What am I missing here?

7
  • 1
    Do you actually have a settings.py file in your project/folder ? Commented May 13, 2017 at 22:20
  • In project/app_folder? No. I am just wondering how come it will have settings. py file(It is an app)? I tried copying it from the main project folder, no luck. Commented May 13, 2017 at 22:23
  • He has it in the project/thesite/ folder: youtu.be/igKeXLDMSqU?t=2m35s Commented May 13, 2017 at 22:24
  • When you see "No module named 'X'", that is almost always something in your settings.py file. Did you accidentally put 'settings' in your setings.py file? Commented May 13, 2017 at 22:40
  • @Timm Simpkins Nope. Commented May 13, 2017 at 22:44

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.