Wizard
Wizard

Reputation: 22093

Rename apps for a minimal project

I'd like to rename myapp from block to article

  1. change the app directory name from block to article
  2. amend the installed apps in setting.py
  3. amend it in app.py
  4. delete migrations and make new

When I try to login admin site, it report

 ModuleNotFoundError: No module named 'article'

I found multiple answer to this question, but they are complicated than I simply re-start a new project.

Is it possible a straight-forwards solution.

The error report:

System check identified no issues (0 silenced).
May 24, 2018 - 07:25:48
Django version 1.11.13, using settings 'forum.settings'
Starting development server at http://127.0.0.1:8001/
Quit the server with CONTROL-C.
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x110fff048>
Traceback (most recent call last):
  File "/Users/me/Desktop/Django/forum/ll_forum/lib/python3.6/site-packages/django/utils/autoreload.py", line 228, in wrapper
    fn(*args, **kwargs)
  File "/Users/me/Desktop/Django/forum/ll_forum/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 116, in inner_run
    autoreload.raise_last_exception()
  File "/Users/me/Desktop/Django/forum/ll_forum/lib/python3.6/site-packages/django/utils/autoreload.py", line 251, in raise_last_exception
    six.reraise(*_exception)
  File "/Users/me/Desktop/Django/forum/ll_forum/lib/python3.6/site-packages/django/utils/six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "/Users/me/Desktop/Django/forum/ll_forum/lib/python3.6/site-packages/django/utils/autoreload.py", line 228, in wrapper
    fn(*args, **kwargs)
  File "/Users/me/Desktop/Django/forum/ll_forum/lib/python3.6/site-packages/django/__init__.py", line 27, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Users/me/Desktop/Django/forum/ll_forum/lib/python3.6/site-packages/django/apps/registry.py", line 85, in populate
    app_config = AppConfig.create(entry)
  File "/Users/me/Desktop/Django/forum/ll_forum/lib/python3.6/site-packages/django/apps/config.py", line 94, in create
    module = import_module(entry)
  File "/Users/me/anaconda3/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'article'
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x105510048>
Traceback (most recent call last):
  File "/Users/me/Desktop/Django/forum/ll_forum/lib/python3.6/site-packages/django/utils/autoreload.py", line 228, in wrapper
    fn(*args, **kwargs)
  File "/Users/me/Desktop/Django/forum/ll_forum/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 116, in inner_run
    autoreload.raise_last_exception()
  File "/Users/me/Desktop/Django/forum/ll_forum/lib/python3.6/site-packages/django/utils/autoreload.py", line 251, in raise_last_exception
    six.reraise(*_exception)
  File "/Users/me/Desktop/Django/forum/ll_forum/lib/python3.6/site-packages/django/utils/six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "/Users/me/Desktop/Django/forum/ll_forum/lib/python3.6/site-packages/django/utils/autoreload.py", line 228, in wrapper
    fn(*args, **kwargs)
  File "/Users/me/Desktop/Django/forum/ll_forum/lib/python3.6/site-packages/django/__init__.py", line 27, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Users/me/Desktop/Django/forum/ll_forum/lib/python3.6/site-packages/django/apps/registry.py", line 85, in populate
    app_config = AppConfig.create(entry)
  File "/Users/me/Desktop/Django/forum/ll_forum/lib/python3.6/site-packages/django/apps/config.py", line 94, in create
    module = import_module(entry)
  File "/Users/me/anaconda3/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'article'

The project file structure is:

In [16]: ! tree -L 2
.
├── article
│   ├── __init__.py
│   ├── __pycache__
│   ├── admin.py
│   ├── apps.py
│   ├── migrations
│   ├── models.py
│   ├── static
│   ├── templates
│   ├── tests.py
│   ├── urls.py
│   └── views.py
├── db.sqlite3
├── forum
│   ├── __init__.py
│   ├── __pycache__
│   ├── settings.py
│   ├── static
│   ├── templates
│   ├── urls.py
│   └── wsgi.py
├── ll_forum
│   ├── bin
│   ├── include
│   ├── lib
│   ├── pip-selfcheck.json
│   ├── pyvenv.cfg
│   └── share
└── manage.py

14 directories, 15 files

The installed apps in setting.py

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    #my apps
    "article"
]

Upvotes: 0

Views: 320

Answers (1)

Gaurav Tomer
Gaurav Tomer

Reputation: 749

You can create a file named -'apps.py' in your app directory and set a different name in 'name' attribute for your app by inheriting django's 'AppConfig' class as follows -

from django.apps import AppConfig

class AppNameConfig(AppConfig): 
    name = 'new name'
    verbose_name = "new verbose name" 

Upvotes: 1

Related Questions