Integration of Django based plug-ins into wagtail - django

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 _

Related

Django ImportError: cannot import name 'python_2_unicode_compatible' from 'django.utils.encoding'

I know others have had a similar issues with and getting this same error, but I think my situation is unique.
I am running Django 3.1.4 and on my local machine, I can run python manage.py shell with no issue.
On the server instance, running what should be the same project, and the same version of Django, I get:
Django ImportError: cannot import name 'python_2_unicode_compatible'
from 'django.utils.encoding'
When trying to run manage.py shell. To make things more cryptic, if I open the shell on my local machine and run:
from django.utils.encoding import python_2_unicode_compatible
I get the same error. So for some reason when I call manage.py shell from my local machine it doesn't try to import python_2_unicode_compatible, but when I run it from the server it does. I can't find where the discrepancy is.
Here is the full stacktrace if that is helpful:
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/chase/Env/mantis/lib/python3.8/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
utility.execute()
File "/home/chase/Env/mantis/lib/python3.8/site-packages/django/core/management/__init__.py", line 377, in execute
django.setup()
File "/home/chase/Env/mantis/lib/python3.8/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/chase/Env/mantis/lib/python3.8/site-packages/django/apps/registry.py", line 114, in populate
app_config.import_models()
File "/home/chase/Env/mantis/lib/python3.8/site-packages/django/apps/config.py", line 211, in import_models
self.models_module = import_module(models_module_name)
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 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 848, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/home/chase/Env/mantis/lib/python3.8/site-packages/request/models.py", line 7, in <module>
from django.utils.encoding import python_2_unicode_compatible
ImportError: cannot import name 'python_2_unicode_compatible' from 'django.utils.encoding' (/home/chase/Env/mantis/lib/python3.8/site-packages/django/utils/encoding.py)
Any ideas on where to start poking around?
You can try this.
First, install six:
pip install six
Then go to the django.utils.encoding.py file and simply import python_2_unicode_compatible from six like this:
from six import python_2_unicode_compatible

Django Celery: TypeError: 'NoneType' object is not iterable

I'm adding django-celery to my project, there's currently an issue you'll experience where a first install of this installs an outdated celery which you need to update and it has a dependency django-celery-results which also has it's expected version for each depending on it's version.
I've ended up with the following versions
Name: django-celery-results
Version: 1.0.0
Name: django-celery
Version: 3.3.1
Name: celery
Version: 4.4.0
in my app/tasks.py I have the following code
from celery import shared_task
from django.conf import settings
#shared_task
def update_extend():
users = User.objects.filter(is_active=True)
for user in users:
....
based on the docs this seems like the next line of code to test
(fortnox) sam#sam:/media/veracrypt1/fortnox$ python manage.py celery
Traceback (most recent call last):
File "manage.py", line 21, in <module>
main()
File "manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "/home/sam/code/envs/fortnox/lib/python3.6/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
utility.execute()
File "/home/sam/code/envs/fortnox/lib/python3.6/site-packages/django/core/management/__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/sam/code/envs/fortnox/lib/python3.6/site-packages/django/core/management/__init__.py", line 244, in fetch_command
klass = load_command_class(app_name, subcommand)
File "/home/sam/code/envs/fortnox/lib/python3.6/site-packages/django/core/management/__init__.py", line 37, in load_command_class
module = import_module('%s.management.commands.%s' % (app_name, name))
File "/home/sam/code/envs/fortnox/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 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/home/sam/code/envs/fortnox/lib/python3.6/site-packages/djcelery/management/commands/celery.py", line 11, in <module>
class Command(CeleryCommand):
File "/home/sam/code/envs/fortnox/lib/python3.6/site-packages/djcelery/management/commands/celery.py", line 16, in Command
tuple(base.get_options()) +
TypeError: 'NoneType' object is not iterable
which throws the error shown above.
django-celery does not support Django versions higher than Django 1.10.* also does not support celery 4.*.*
From Celery documentation for Django
Previous versions of Celery required a separate library to work with
Django, but since 3.1 this is no longer the case. Django is supported
out of the box now so this document only contains a basic way to
integrate Celery and Django. You’ll use the same API as non-Django
users so you’re recommended to read the First Steps with Celery
tutorial first and come back to this tutorial. When you have a working
example you can continue to the Next Steps guide.
You can still use django-celery-results but withouth needs of djcelery

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

(django) Why do I get an error when adding app to INSTALLED_APPS but not when just importing?

I wrote an app with abstract classes, some utils functions, my common template tags and so on.
Somewere I saw it's recommended way to do this stuff to a reusable app. (It's all related to each other.) Why not? It seems to be a good idea. So I made the stuff being part of an app. Since that app has no urls, views and not the normal structur, it's more like a python module. (But it needs django.)
When I import this app the normal way (with import myapp on top of the files) it works fine.
Looking for bestpractice I saw in the official django tutorial part 8 that's recommended to import apps in INSTALLED_APPS in settings.py. Since I made my utils module being an app I thought I could just add a line to INSTALLED_APPS. Just like this: myapp.apps.MyappConfig.
My project structure:
project
|-... [multiple normal apps]
|-myapp [which is my utils app]
|-project [the inner folder]
|-... [some other stuff]
As you can see, myapp, the utils app, is still part of the project (and hasn't been made a reusable app yet.)
But when I add that line to INSTALLED_APPS I get the following traceback:
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0xb6356adc>
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/django/utils/autoreload.py", line 227, in wrapper
fn(*args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/django/core/management/commands/runserver.py", line 117, in inner_run
autoreload.raise_last_exception()
File "/usr/local/lib/python3.5/dist-packages/django/utils/autoreload.py", line 250, in raise_last_exception
six.reraise(*_exception)
File "/usr/local/lib/python3.5/dist-packages/django/utils/six.py", line 685, in reraise
raise value.with_traceback(tb)
File "/usr/local/lib/python3.5/dist-packages/django/utils/autoreload.py", line 227, in wrapper
fn(*args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/django/__init__.py", line 27, in setup
apps.populate(settings.INSTALLED_APPS)
File "/usr/local/lib/python3.5/dist-packages/django/apps/registry.py", line 85, in populate
app_config = AppConfig.create(entry)
File "/usr/local/lib/python3.5/dist-packages/django/apps/config.py", line 94, in create
module = import_module(entry)
File "/usr/lib/python3.5/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 986, in _gcd_import
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 944, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
File "<frozen importlib._bootstrap>", line 986, in _gcd_import
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 944, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
File "<frozen importlib._bootstrap>", line 986, in _gcd_import
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 665, in exec_module
File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
File "/my_cool_directory/project/myapp/__init__.py", line 1, in <module>
from .models import *
File "/my_cool_directory/project/myapp/models.py", line 8, in <module>
class Publishable(models.Model):
File "/usr/local/lib/python3.5/dist-packages/django/db/models/base.py", line 110, in __new__
app_config = apps.get_containing_app_config(module)
File "/usr/local/lib/python3.5/dist-packages/django/apps/registry.py", line 247, in get_containing_app_config
self.check_apps_ready()
File "/usr/local/lib/python3.5/dist-packages/django/apps/registry.py", line 125, in check_apps_ready
raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
It's not like I have I serious problem because I can import the app the normal way. I'm just curious: Why isn't it working the recommended way?
And: how to make things in submodules to be importable from topmodule? In apps.py? Or in __init__.py?
You cannot import your models into the root module of your app. That's one of the limitations of Django's application framework. You need to remove the from .models import * line from myapp/__init__.py.
Django first imports the root module of every installed application. This is used to set up the application configs, which are needed to set up relational fields between models in different apps. To set up these relations, both of the relevant apps must've been loaded.
Django learned the hard way that allowing models to be imported at any time greatly complicated this process, and this introduced a range of hard-to-solve bugs. In 1.7 this process was greatly simplified, reducing the number of bugs, but in that simplification it was decided that all applications must've been loaded before any model can be imported.