Categories
Bootstrap Development Django MySQL

How do I declare new app in Django rest framework? It seems everything is OK but i have RuntimeError

I want to declare new app in python using Django rest framework.

First I did below steps:

cmd>django-admin startproject djrest .
cmd>cd djrest
cmd>django-admin startapp sarox
cmd>cd..

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'sarox',
        'USER': 'root',
        'PASSWORD': '*******',
        'HOST': 'localhost',
        'PORT': '****',
    }
}

cmd>python manage.py migrate
cmd>python manage.py createsuperuser
cmd>py manage.py runserver 0.0.0.0:2281

then I declared a model in Sarox_models.py under sarox directory:

from django.db import models
class SaroxUsers(models.Model):
    user_id = models.AutoField(db_column='User_id', primary_key=True)  # Field name made lowercase.
    salt = models.CharField(db_column='Salt', max_length=200, blank=True, null=True)  # Field name made lowercase.
    hash = models.CharField(db_column='Hash', max_length=200, blank=True, null=True)  # Field name made lowercase.
    username = models.CharField(db_column='Username', unique=True, max_length=15)  # Field name made lowercase.

    class Meta:
        managed = False
        db_table = 'sarox_users'

Then I added below line to views.py under sarox directory:

from djrest.sarox.Sarox_models import SaroxUsers

and then create a line in INSTALLED_APPS configuration class in the settings.py file.

Here is my INSTALLED_APPS configuration class:

INSTALLED_APPS = [
    'sarox.apps.SaroxConfig',
    ...
]

My new app declared like this in sarox/apps.py

from django.apps import AppConfig
class SaroxConfig(AppConfig):
    name = 'sarox'

But when I run python manage.py runserver 0.0.0.0:2281 command it raises an run time error:

E:\MyApps\Plot\djrest>python manage.py runserver 0.0.0.0:2281
Watching for file changes with StatReloader
Exception in thread django-main-thread:
Traceback (most recent call last):
  File "C:\Users\AMoha\AppData\Local\Programs\Python\Python37\lib\threading.py", line 926, in _bootstrap_inner
    self.run()
  File "C:\Users\AMoha\AppData\Local\Programs\Python\Python37\lib\threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\AMoha\AppData\Local\Programs\Python\Python37\lib\site-packages\django\utils\autoreload.py", line 54, in wrapper
    fn(*args, **kwargs)
  File "C:\Users\AMoha\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\management\commands\runserver.py", line 109, in inner_run
    autoreload.raise_last_exception()
  File "C:\Users\AMoha\AppData\Local\Programs\Python\Python37\lib\site-packages\django\utils\autoreload.py", line 77, in raise_last_exception
    raise _exception[1]
  File "C:\Users\AMoha\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\management\__init__.py", line 337, in execute
    autoreload.check_errors(django.setup)()
  File "C:\Users\AMoha\AppData\Local\Programs\Python\Python37\lib\site-packages\django\utils\autoreload.py", line 54, in wrapper
    fn(*args, **kwargs)
  File "C:\Users\AMoha\AppData\Local\Programs\Python\Python37\lib\site-packages\django\__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "C:\Users\AMoha\AppData\Local\Programs\Python\Python37\lib\site-packages\django\apps\registry.py", line 91, in populate
    app_config = AppConfig.create(entry)
  File "C:\Users\AMoha\AppData\Local\Programs\Python\Python37\lib\site-packages\django\apps\config.py", line 116, in create
    mod = import_module(mod_path)
  File "C:\Users\AMoha\AppData\Local\Programs\Python\Python37\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 965, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'sarox'
...

And when I removed the new line contains 'sarox.apps.SaroxConfig', in the INSTALLED_APPS class configuration of settings.py everything became OK.

my project directory structure is here:

Do you have any idea?

Leave a Reply

Your email address will not be published. Required fields are marked *