How to access a Django model outside a Django project? - django

The problem below is raised after reviewing all the related SO questions, but unfortunately none of the answers brought a solution in my case (similar problem like: How to access Django models outside of Django?).
(with Django version: 4.1.1)
Having an application "simo" available with the related model, the db access works well from Django shell; but trying to access to it from outside Django as "scheduling.py", it just does not work.
import os
import django
os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'
django.setup()
from django.db import models
from simo.models import Tasks
I tried to execute my python script in any folder:
beside manage.py,
beside settings.py and
beside models.py but none of them works.
[mysite]
[mysite]
[simo]
[migrations]
views.py
models.py
urls.py
__init__.py
apps.py
tests.py
admin.py
settings.py
urls.py
__init__.py
wsgi.py
asgi.py
scheduling.py
__init__.py
db.sqlite3
manage.py
The error received is:
C:\...\python.exe C:/.../mysite/scheduling.py
Traceback (most recent call last):
File "C:\...\mysite\scheduling.py", line 5, in <module>
django.setup()
File "C:\...\lib\site-packages\django\__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\...\lib\site-packages\django\apps\registry.py", line 91, in populate
app_config = AppConfig.create(entry)
File "C:\...\lib\site-packages\django\apps\config.py", line 178, in create
mod = import_module(mod_path)
File "C:\...\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 972, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 984, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'simo'
Process finished with exit code 1
Could anyone advise me on how to resolve this issue?

Try calling
from django.db import models and from simo.models import Tasks before django.setup()

Related

how to fix this error: No module named 'allauth'?

I want to use allauth in django API but when I installed it I got some errors I fixed it by django.setup() and after that, I get the error which tells me that I have to set DJANGO_SETTINGS_MODULE now I did that in manage.py:
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys
import django
def main():
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'website.settings')
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)
if __name__ == '__main__':
os.environ['DJANGO_SETTINGS_MODULE'] = 'website.settings'
django.setup()
main()
but I get this Traceback:
Traceback (most recent call last):
File "manage.py", line 23, in <module>
django.setup()
File "/home/abdelhamedabdin/.local/lib/python3.8/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/abdelhamedabdin/.local/lib/python3.8/site-packages/django/apps/registry.py", line 91, in populate
app_config = AppConfig.create(entry)
File "/home/abdelhamedabdin/.local/lib/python3.8/site-packages/django/apps/config.py", line 90, in create
module = import_module(entry)
File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 973, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'allauth'
please can anyone tells me how can I fix that exception error? I did many searches but I get no help and I asked here but also I got no help so, please if anyone can help me!
Additional information:
that is what I did in settings.py after I installed allauth by pip3
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.sites',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'allauth',
'allauth.account',
'allauth.socialaccount',
'allauth.account.auth_backends.AuthenticationBackend',
'index.apps.IndexConfig',
'account.apps.AccountConfig',
'community.apps.CommunityConfig',
]
AUTHENTICATION_BACKENDS = (
"django.contrib.auth.backends.ModelBackend",
"allauth.account.auth_backends.AuthenticationBackend",
)
SITE_ID = 1
and in urls.py
path('accounts/', include('allauth.urls')),
Edit post:
by using virtualenv that Trace has raised:
Traceback (most recent call last):
File "manage.py", line 23, in <module>
django.setup()
File "/media/abdelhamedabdin/BE4C6BE74C6B98C3/Cources/Django/all git/ripo/website/venv/lib/python3.8/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/media/abdelhamedabdin/BE4C6BE74C6B98C3/Cources/Django/all git/ripo/website/venv/lib/python3.8/site-packages/django/apps/registry.py", line 91, in populate
app_config = AppConfig.create(entry)
File "/media/abdelhamedabdin/BE4C6BE74C6B98C3/Cources/Django/all git/ripo/website/venv/lib/python3.8/site-packages/django/apps/config.py", line 90, in create
module = import_module(entry)
File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 961, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 783, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/media/abdelhamedabdin/BE4C6BE74C6B98C3/Cources/Django/all git/ripo/website/venv/lib/python3.8/site-packages/allauth/account/auth_backends.py", line 3, in <module>
from django.contrib.auth.backends import ModelBackend
File "/media/abdelhamedabdin/BE4C6BE74C6B98C3/Cources/Django/all git/ripo/website/venv/lib/python3.8/site-packages/django/contrib/auth/backends.py", line 5, in <module>
from django.contrib.auth.models import Permission
File "/media/abdelhamedabdin/BE4C6BE74C6B98C3/Cources/Django/all git/ripo/website/venv/lib/python3.8/site-packages/django/contrib/auth/models.py", line 2, in <module>
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File "/media/abdelhamedabdin/BE4C6BE74C6B98C3/Cources/Django/all git/ripo/website/venv/lib/python3.8/site-packages/django/contrib/auth/base_user.py", line 47, in <module>
class AbstractBaseUser(models.Model):
File "/media/abdelhamedabdin/BE4C6BE74C6B98C3/Cources/Django/all git/ripo/website/venv/lib/python3.8/site-packages/django/db/models/base.py", line 107, in __new__
app_config = apps.get_containing_app_config(module)
File "/media/abdelhamedabdin/BE4C6BE74C6B98C3/Cources/Django/all git/ripo/website/venv/lib/python3.8/site-packages/django/apps/registry.py", line 252, in get_containing_app_config
self.check_apps_ready()
File "/media/abdelhamedabdin/BE4C6BE74C6B98C3/Cources/Django/all git/ripo/website/venv/lib/python3.8/site-packages/django/apps/registry.py", line 135, in check_apps_ready
raise AppRegistryNotReady("Apps aren't loaded yet.")
By installing django-allauth as follows:
python -m pip install django-allauth
I solved my problem and the main problem was spin around allauth.account that I want to migrate it into INSTALLED_APPS also, I had an additional app in this case, 'allauth.account.auth_backends.AuthenticationBackend', those apps was the reason for the problems.
for the first app (allauth.account.auth_backends.AuthenticationBackend)I was getting an error that tells me that I am not able to migrate the INSTALLED_APPS without migrating allauth app first.
for the second app (allauth.account) I was getting an error duplicate 'account' because I have already application with that name so, I had to rename the app name which already exists to name like accounts for example until I can install allauth.account succeeded.
until I am able to specify one error of each app I was migrating each one separately to show the specific error to it but when I was migrating the both together I was getting that Traceback I specified above.

django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet when loading wsgi.py

I have a problem with Django and wsgi that I cannot pinpoint. The app works fine on my local test server and it also works fine on a local apache WAMP setup (without any venvs). When deploying it to our Linux server again the local test server runs (as does makemigrations, migrate or check):
python3 manage.py runserver
/home/www-test/myapp-venv/lib/python3.7/site-packages/requests/__init__.py:91: RequestsDependencyWarning: urllib3 (1.25.8) or chardet (2.0.3) doesn't match a supported version!
RequestsDependencyWarning)
/home/www-test/myapp-venv/lib/python3.7/site-packages/requests/__init__.py:91: RequestsDependencyWarning: urllib3 (1.25.8) or chardet (2.0.3) doesn't match a supported version!
RequestsDependencyWarning)
Performing system checks...
System check identified no issues (0 silenced).
March 18, 2020 - 16:22:20
Django version 2.2.11, using settings 'myapp.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
python3 manage.py check
/home/www-test/myapp-venv/lib/python3.7/site-packages/requests/__init__.py:91:
RequestsDependencyWarning: urllib3 (1.25.8) or chardet (2.0.3) doesn't match a supported version!
RequestsDependencyWarning)
System check identified no issues (0 silenced).
However when I try to deploy it with wsgi/Apache on the Linux machine I get
Traceback (most recent call last):
File "myapp/wsgi.py", line 20, in <module>
application = get_wsgi_application()
File "/home/www-test/.local/lib/python3.7/site-packages/django/core/wsgi.py", line 12, in get_wsgi_application
django.setup(set_prefix=False)
File "/home/www-test/.local/lib/python3.7/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/www-test/.local/lib/python3.7/site-packages/django/apps/registry.py", line 91, in populate
app_config = AppConfig.create(entry)
File "/home/www-test/.local/lib/python3.7/site-packages/django/apps/config.py", line 90, in create
module = import_module(entry)
File "/usr/lib/python3.7/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 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 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/home/www-test/myapp/myapp/maintenance.py", line 9, in <module>
from imageplus.models import ImagePlus
File "/home/www-test/myapp/imageplus/models.py", line 6, in <module>
from userprofile.models import Userprofile
File "/home/www-test/myapp/userprofile/models.py", line 5, in <module>
from django.contrib.auth.models import User
File "/home/www-test/.local/lib/python3.7/site-packages/django/contrib/auth/models.py", line 2, in <module>
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File "/home/www-test/.local/lib/python3.7/site-packages/django/contrib/auth/base_user.py", line 47, in <module>
class AbstractBaseUser(models.Model):
File "/home/www-test/.local/lib/python3.7/site-packages/django/db/models/base.py", line 103, in __new__
app_config = apps.get_containing_app_config(module)
File "/home/www-test/.local/lib/python3.7/site-packages/django/apps/registry.py", line 252, in get_containing_app_config
self.check_apps_ready()
File "/home/www-test/.local/lib/python3.7/site-packages/django/apps/registry.py", line 135, in check_apps_ready
raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
I suspect it has something to do with venv or with missing paths, but I don't know where to start. wsgi.py looks like this:
"""
WSGI config for myapp project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/2.2/howto/deployment/wsgi/
"""
import os, sys
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myapp.settings')
sys.path.append('/home/www-test/myapp/myapp')
sys.path.append('/home/www-test/myapp')
sys.path.append('/home/www-test/myapp-venv/lib/python3.7/site-packages')
application = get_wsgi_application()
Any suggestions a to where to even start with hunting this error would be very much appreciated.
[edit:]
I traced the error further to apps.py of myapp which looks like this:
from django.apps import AppConfig
from threading import Thread
from django.utils import timezone
import time
import schedule
import os
from decouple import config
class SebastianConfig(AppConfig):
name = 'myapp'
def ready(self):
if os.environ.get('RUN_MAIN', None) == 'true':
return
import myapp.signals
if config('AUTOMAINTENANCE', cast=bool)==False:
print ('[myApp]: auto maintenance is disabled')
return
from myapp.maintenance import maintenance
def scheduler_demon():
while True:
schedule.run_pending()
time.sleep(60)
def aliveping():
print(f'[myApp]:ping {timezone.now()}')
schedule.every(5).minutes.do(aliveping)
schedule.every(24).hours.do(maintenance)
schedule.run_all()
worker=Thread(target=scheduler_demon,args=(),)
worker.daemon=True
worker.start()
In theory this should not cause problems as fas as my understanding of Django goes. Its also the ONLY spot maintenance is imported.
Now the funny thing is I removed that passage and wsgi worked like a charm. I added the passage once again and SURPRISE it still works like a charm?!!?

django-admin "ModuleNotFoundError:" after change in settings structure for development & production

Background
I have recently started to learn Python Django. I read that it was good practice to have separate settings file for different environments. Consequently I have tried to implement something similar to what is describe in the "Simple Package Organization for Environments" section of this wiki: https://code.djangoproject.com/wiki/SplitSettings
Problem
When I now run a django-admin command I get a ModuleNotFoundError. Below I have copy pasted the error log I get for "django-admin check --deploy". "python manage.py runserver --settings=CollegeComp.settings.development" works fine.
Things I've tried
I was reading that I may have to reset the DJANGO_SETTINGS_MODULE environment variable in my virtual environment. I entered "set DJANGO_SETTINGS_MODULE=CollegeComp.settings.development" but I still get the same error.
Python path
When I type the following in the shell with my virtual environment activated:
import sys
print(sys.path)
I get the following:
['C:\\Users\\myusername\\Documents\\UdemyDjango\\MyPersonalProject\\College-Project-master\\CollegeComp',
'C:\\Users\\myusername\\Anaconda3\\envs\\MyDjangoEnv\\python37.zip',
'C:\\Users\\myusername\\Anaconda3\\envs\\MyDjangoEnv\\DLLs',
'C:\\Users\\myusername\\Anaconda3\\envs\\MyDjangoEnv\\lib',
'C:\\Users\\myusername\\Anaconda3\\envs\\MyDjangoEnv',
'C:\\Users\\myusername\\Anaconda3\\envs\\MyDjangoEnv\\lib\\site-packages']
Error log
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\myusername\Anaconda3\envs\MyDjangoEnv\Scripts\django-admin-script.py", line 10, in <module>
sys.exit(execute_from_command_line())
File "C:\Users\myusername\Anaconda3\envs\MyDjangoEnv\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
utility.execute()
File "C:\Users\myusername\Anaconda3\envs\MyDjangoEnv\lib\site-packages\django\core\management\__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\myusername\Anaconda3\envs\MyDjangoEnv\lib\site-packages\django\core\management\base.py", line 329, in run_from_argv
connections.close_all()
File "C:\Users\myusername\Anaconda3\envs\MyDjangoEnv\lib\site-packages\django\db\utils.py", line 220, in close_all
for alias in self:
File "C:\Users\myusername\Anaconda3\envs\MyDjangoEnv\lib\site-packages\django\db\utils.py", line 214, in __iter__
return iter(self.databases)
File "C:\Users\myusername\Anaconda3\envs\MyDjangoEnv\lib\site-packages\django\utils\functional.py", line 37, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\myusername\Anaconda3\envs\MyDjangoEnv\lib\site-packages\django\db\utils.py", line 147, in databases
self._databases = settings.DATABASES
File "C:\Users\myusername\Anaconda3\envs\MyDjangoEnv\lib\site-packages\django\conf\__init__.py", line 57, in __getattr__
self._setup(name)
File "C:\Users\myusername\Anaconda3\envs\MyDjangoEnv\lib\site-packages\django\conf\__init__.py", line 44, in _setup
self._wrapped = Settings(settings_module)
File "C:\Users\myusername\Anaconda3\envs\MyDjangoEnv\lib\site-packages\django\conf\__init__.py", line 107, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "C:\Users\myusername\Anaconda3\envs\MyDjangoEnv\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 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 'CollegeComp'
My project directory
CollegeComp
|-CollegeComp
|-settings
|-__init__.py
|-base.py
|-development.py
|-local.py
|-production.py
|-__init__.py
|-urls.py
|-wsgi.py
|-<my apps>
base.py
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
<rest of standard Django settings.py code>
development.py
from CollegeComp.settings.base import *
#Override base.py settings here
DEBUG = True
#Import local setting file
try:
from CollegeComp.settings.local import *
except:
pass
local.py
from CollegeComp.settings.base import *
#Override base settings here
DEBUG = True
production.py
from CollegeComp.settings.base import *
#Override base settings here
DEBUG = False
#Import local setting file
try:
from CollegeComp.settings.local import *
except:
pass
System info
Django version 2.1
Operating system: Windows 10
Python version: 3.7.0
I am grateful for any help I can get.

Django: Changing project folder / settings structure

I am currently trying to change from the default Django structure to that one.
I now copied all the files in the new folders, but when running python manage.py runserver --settings=settings.local it shows the following in my terminal:
Traceback (most recent call last):
File "manage.py", line 16, in <module>
execute_from_command_line(sys.argv)
File "/Users/Marc/.local/share/virtualenvs/lumis-vJ5Odiz7/lib/python3.6/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
utility.execute()
File "/Users/Marc/.local/share/virtualenvs/lumis-vJ5Odiz7/lib/python3.6/site-packages/django/core/management/__init__.py", line 317, in execute
settings.INSTALLED_APPS
File "/Users/Marc/.local/share/virtualenvs/lumis-vJ5Odiz7/lib/python3.6/site-packages/django/conf/__init__.py", line 56, in __getattr__
self._setup(name)
File "/Users/Marc/.local/share/virtualenvs/lumis-vJ5Odiz7/lib/python3.6/site-packages/django/conf/__init__.py", line 43, in _setup
self._wrapped = Settings(settings_module)
File "/Users/Marc/.local/share/virtualenvs/lumis-vJ5Odiz7/lib/python3.6/site-packages/django/conf/__init__.py", line 106, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "/Users/Marc/.local/share/virtualenvs/lumis-vJ5Odiz7/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 941, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
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 'settings'
Can anyone see what I am doing wrong?
I also changed in my wsgi.py and manage.py to the following:
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings")
You need to turn the config/ directory into a python module by creating a blank config/__init__.py file, then configure your DJANGO_SETTINGS_MODULE like this in manage.py and config/wsgi.py:
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings.local")
Then you should be able to just run ./manage.py runserver without the --settings option.
You need to tell django where to look for settings module therefore you have to specify the module path in manage.py:
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings")
and in __init__.py inside settings folder add the following lines:
try:
from config.settings.local import *
except:
pass
if you are using Pycharm go to
File > Settings > Language and Frameworks >Django
and configure that
1)[checked] Django support
2) settings: config\settings.py
3)manage script: manage.py
But if you are using a independent console it should be works fine just with the ___init__.py file in the folder config ,and the configuration os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings") in the manage.py file,using
python manage.py runserver

Integration of Django based plug-ins into wagtail

Can anyone describe the general actions when integrating addons or widgets built originally for django in to wagtail? For a novice user, it's hard to understand exactly how to edit the code so that it works in wagtail cms.
For example, I'm trying to integrate two very useful Django addons(widgets) into my project and I encounter in both cases the problem, when it comes to customizing and inserting code into settings.py
In both cases i have to customize some code but we don't have settings.py in wagtail and if ill try to put this code:
COUNTRIES_OVERRIDE = {
'XZ': _('Tibet')
}
in to base.py I'm getting an error:
(env) C:\Users\xakep\Documents\GitHub\database\blemmy>manage.py runserver
Traceback (most recent call last):
File "C:\Users\xakep\Documents\GitHub\database\blemmy\manage.py",
line 10, in <module> execute_from_command_line(sys.argv)
File "C:\Python36\lib\site-packages\django\core\management\__init__.py",
line 363, in execute_from_command_line utility.execute()
File "C:\Python36\lib\site-packages\django\core\management\__init__.py",
line 307, in execute settings.INSTALLED_APPS
File "C:\Python36\lib\site-packages\django\conf\__init__.py", line 56,
in__getattr__self._setup(name)
File "C:\Python36\lib\site-packages\django\conf\__init__.py", line 41,
in _setup self._wrapped = Settings(settings_module)
File "C:\Python36\lib\site-packages\django\conf\__init__.py", line 110,
in__init__ mod = importlib.import_module(self.SETTINGS_MODULE)
File "C:\Python36\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 978, in _gcd_import
File "<frozen importlib._bootstrap>", line 961, in _find_and_load
File "<frozen importlib._bootstrap>", line 950, in
_find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 205, in
_call_with_frames_removed
File "C:\Users\xakep\Documents\GitHub\database\blemmy\blemmy\settings\
__init__.py", line 1, in <module> from .dev import *
File "C:\Users\xakep\Documents\GitHub\database\blemmy\blemmy\settings\
dev.py", line 2, in <module> from .base import *
File "C:\Users\xakep\Documents\GitHub\database\blemmy\blemmy\settings\
base.py", line 112, in <module> 'XZ': _('Tibet')
NameError: name '_' is not defined
If you can help(explane) me how to use similar projects in wagtail i have tons of good ideas =)
Thanks a lot!!!
base.py is the correct location for settings like this. In the Wagtail project structure, the settings are split up into dev.py (for settings only used in the development environment), production.py (for settings only used in production), and base.py (common to both), as described here: http://docs.wagtail.io/en/v1.11.1/reference/project_template.html#django-settings
The name '_' is not defined error isn't related to Wagtail - it's a detail that's missing from the django-countries documentation. Defining a function called _ is a standard convention for marking strings as translatable, described in the Django docs here: https://docs.djangoproject.com/en/1.11/topics/i18n/translation/
To fix it, add the following line to the top of your settings file:
from django.utils.translation import ugettext_lazy as _