8

I just started learning Django. My main source of knowledge about that framework is the book "Python Web Development with Django" by Rudolph Snellius, Jeff Forcier and Wesley Chun.

Can you explain to me how to use SQlite in Django? The book is a few years old, which is why I have some problems with configuring the database.

The first step I have done correctly was to create an app with the command ./manage.py startapp blog, then I added verse mojprojekt to tuple INSTALLED_APPS in setting.py file. Then I have to add verses DATABASE_ENGINE = 'sqlite3' and DATABASE_NAME = 'path to my project' to settings.py.

The last step is to run the command ./manage.py syncdb. After this I get an error:

konrad@konrad-MS-7823:~/Documents/Django/mojprojekt$ ./manage.py syncdb
Traceback (most recent call last):
  File "./manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/lib/python2.7/dist-packages/django/core/management/__init__.py", line 354, in execute_from_command_line
    utility.execute()
  File "/usr/lib/python2.7/dist-packages/django/core/management/__init__.py", line 328, in execute
    django.setup()
  File "/usr/lib/python2.7/dist-packages/django/__init__.py", line 18, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/usr/lib/python2.7/dist-packages/django/apps/registry.py", line 85, in populate
    app_config = AppConfig.create(entry)
  File "/usr/lib/python2.7/dist-packages/django/apps/config.py", line 119, in create
    import_module(entry)
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
ImportError: No module named blog
konrad@konrad-MS-7823:~/Documents/Django/mojprojekt$ 

Could you help me if you know where I made a mistake?

Update: Here is my application definition:

    # Application definition

    INSTALLED_APPS = (
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
        'mojprojekt.blog',

)

This still gives the same result.

2
  • in INSTALLED_APPS should be the same name as in ./manage.py startapp, so if you startapp blog, it should be blog, but not the mojproject Commented Jan 30, 2017 at 21:59
  • 1
    If it's a few years old don't bother with it. Django has evolved a great deal. Your app should be just 'blog' not 'myproject.blog' Commented Jan 31, 2017 at 5:19

1 Answer 1

17

Based on the stacktrace you are receiving, it looks like blog is not in your settings file. Check settings.py, under INSTALLED_APPS and add a new entry for 'blog'.

As far as setting up the database in the latest version of Django, You should have a look at this page from the django settings documentation. According to it, the current style for the DATABASE setting is as follows.

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': 'mydatabase', # This is where you put the name of the db file. 
                 # If one doesn't exist, it will be created at migration time.
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

For sqlite ENGINEs, the 'NAME' is actually a path, so you can put your database-in-a-file wherever you like.

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.