Django-Tinymce Import Error - python

I am trying to follow this tutorial on getting tinymce working with django and zinnia. It's not working, so I am attempting to do "Testing" but get this error when I run django-admin.py syncdb. How do I fix this?
$django-admin.py syncdb
Traceback (most recent call last):
File "/usr/local/bin/django-admin.py", line 5, in <module>
pkg_resources.run_script('Django==1.5.1', 'django-admin.py')
File "/usr/lib/python2.7/dist-packages/pkg_resources.py", line 505, in run_script
self.require(requires)[0].run_script(script_name, ns)
File "/usr/lib/python2.7/dist-packages/pkg_resources.py", line 1245, in run_script
execfile(script_filename, namespace, namespace)
File "/usr/local/lib/python2.7/dist-packages/Django-1.5.1-py2.7.egg/EGG-INFO/scripts/django-admin.py", line 5, in <module>
management.execute_from_command_line()
File "/usr/local/lib/python2.7/dist-packages/Django-1.5.1-py2.7.egg/django/core/management/__init__.py", line 453, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python2.7/dist-packages/Django-1.5.1-py2.7.egg/django/core/management/__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python2.7/dist-packages/Django-1.5.1-py2.7.egg/django/core/management/__init__.py", line 263, in fetch_command
app_name = get_commands()[subcommand]
File "/usr/local/lib/python2.7/dist-packages/Django-1.5.1-py2.7.egg/django/core/management/__init__.py", line 109, in get_commands
apps = settings.INSTALLED_APPS
File "/usr/local/lib/python2.7/dist-packages/Django-1.5.1-py2.7.egg/django/conf/__init__.py", line 53, in __getattr__
self._setup(name)
File "/usr/local/lib/python2.7/dist-packages/Django-1.5.1-py2.7.egg/django/conf/__init__.py", line 48, in _setup
self._wrapped = Settings(settings_module)
File "/usr/local/lib/python2.7/dist-packages/Django-1.5.1-py2.7.egg/django/conf/__init__.py", line 134, in __init__
raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e))
ImportError: Could not import settings 'testtinymce.staticfiles_settings' (Is it on sys.path?): No module named staticfiles_settings
Thank you.

I found out the django-tinymce documentation is outdated, i.e. partially wrong.
What I discovered is that different versions of tinymce and django-tinymce packages are not compatible.
I solved it adding some variables to my project/settings.py and altering the tinymce directory and file names.
django-tinymce urls.py had some hardcoded paths in it which assumed the directories were named "tiny_mce" when in reality they were named "tinymce", hence I had to rename them, or alternatively you can change the hardcoded paths in django-tinymce's urls.py.
# project setting.py
STATIC_ROOT = os.path.join(BASE_DIR, "static")
STATIC_JS_DIR = os.path.join(STATIC_DIR, "js")
TINYMCE_JS_ROOT = os.path.join(STATIC_JS_DIR, "tiny_mce")
TINYMCE_JS_URL = os.path.join(TINYMCE_JS_ROOT, "tiny_mce.js")
#TINYMCE_JS_ROOT = os.path.join(STATIC_JS_DIR, "tiny_mce")
#TINYMCE_JS_URL = os.path.join(TINYMCE_JS_ROOT, "tiny_mce.js")

A simple shutdown of the terminal, then restarting the app again fixed it for me (without needing to configure anything extra). I followed the instructions here:
pip install django-tinymce
Add tinymce to the INSTALLED_APPS of 'settings.py'
Add (r'^tinymce/', include('tinymce.urls')), to the urlpatterns in urls.py
Do a python manage.py syncdb (not sure if this is needed)
In terminal: $ export DJANGO_SETTINGS_MODULE='testtinymce.staticfiles_settings'
Do another python manage.py syncdb just in case and then a python manage.py runserver
I then received the error when I tried to open up the browser to: http://localhost:8000/admin/myapphere
I restarted the terminal, did a 'collect static' just in case, then did python manage.py runserver and it worked (I was able to see the new fields)

The latest version of tinymce has a different configuration.
instead of Importing HTMLField like
from tinymce import HTMLField
but rather
from tinymce.models import HTMLField

Related

How do I use django models.py classes in my own python programs?

So, I want to make some changes in the database using python outsite Django, but I am unable to.
I'm trying to import the model from models.py but i'm unable to.
from models import NumberInfo
There is this error:
Traceback (most recent call last):
File "/home/sagan/p/matematika/matematika/mnumbers/prime_insert.py", line 1, in <module>
from models import NumberInfo
File "/home/sagan/p/matematika/matematika/mnumbers/models.py", line 5, in <module>
class NumberInfo(models.Model):
File "/home/sagan/.local/lib/python3.9/site-packages/django/db/models/base.py", line 108, in __new__
app_config = apps.get_containing_app_config(module)
File "/home/sagan/.local/lib/python3.9/site-packages/django/apps/registry.py", line 253, in get_containing_app_config
self.check_apps_ready()
File "/home/sagan/.local/lib/python3.9/site-packages/django/apps/registry.py", line 135, in check_apps_ready
settings.INSTALLED_APPS
File "/home/sagan/.local/lib/python3.9/site-packages/django/conf/__init__.py", line 82, in __getattr__
self._setup(name)
File "/home/sagan/.local/lib/python3.9/site-packages/django/conf/__init__.py", line 69, in _setup
self._wrapped = Settings(settings_module)
File "/home/sagan/.local/lib/python3.9/site-packages/django/conf/__init__.py", line 170, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "/usr/lib/python3.9/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
ModuleNotFoundError: No module named 'matematika'```
If you want to edit database structure, just edit your models in models.py file and after that run
# creating new migrations based on the changes you have made to your models
$ python manage.py makemigrations
# applying and unapplying migrations
$ python manage.py migrate
Visit Django Documentation on Migrations to find out more!
Make sure you import the django python module and load the settings from the model's project before you try to import the model:
# add the django project to python's path so python can find it
# the variable path_of_python_project is the full path of the django project folder
sys.path.append(path_of_django_project)
# load django and the settings from your project
# I'm pretending your project is called django_projectname, you'll want to update that
import django
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'django_projectname.settings')
# setup django
django.setup()
# import your model and use it, just like you would in django
from django_projectname.models import NumberInfo
...

AppEngine application using Django fails to load

Django is constantly causing our application to crash. After deployment the application is running fine, but once the initial instance is restarted/shutdown it often fails to start with an error similar to the following:
Traceback (most recent call last): File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 266, in Handle
result = handler(dict(self._environ), self._StartResponse)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/django-1.5/django/core/handlers/wsgi.py", line 236, in call
self.load_middleware()
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/django-1.5/django/core/handlers/base.py", line 53, in load_middleware
raise exceptions.ImproperlyConfigured('Error importing middleware %s: "%s"' % (mw_module, e))
ImproperlyConfigured: Error importing middleware myfolder.middleware: "No module named myfolder.middleware"
Our file structure is similar to this:
|- app.yaml
|- _ _ init _ _.py
|- settings.py
|- myfolder |
| |- _ _ init _ _.py
| |- middleware.py
| |- ...
|-...
|
Our app.yaml:
application: XXXXX
module: app
version: master
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /api/(login|logout|passwd|master.|banners.)
script: app.handler
secure: always
...
builtins:
- django_wsgi: on
libraries:
- name: django
version: 1.5
env_variables:
DJANGO_SETTINGS_MODULE: 'settings'
We have 2 modules in our application and they both exhibit this behaviour (they have similar configurations). Sometimes the modules will stay up for a whole day before crashing again. After they fail to load, all subsequent requests fail with he same error. Deploying one more time always solves the problem temporarily.
We are using plain django with CloudSql. The problem is not reproducible in the development server. After deployment everything in both modules works fine. All middleware, ndb, memcache, cloudsql, taskqueue, etc, including all the modules inside the "myfolder" and every other library xcopied.
The following attempts at solving this problem haven't worked:
We have tried using the appengine_config.py to force django to reload the settings with from django.conf import settings\nsettings._target = None\n
Originally we had shared settings inside "myfolder" and were importing them with "from myfolder.shared_settings import *" inside the root settings.py but django could not load the module myfolder.shared_settings either (similar problem)
using a custom mysettings.py and defining the DJANGO_SETTINGS_MODULE in the app.yaml or in python
The system is not live yet but will be soon and we are running out of options.
Other traces of similarly failing configurations:
Traceback (most recent call last):
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 266, in Handle
result = handler(dict(self._environ), self._StartResponse)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/django-1.5/django/core/handlers/wsgi.py", line 236, in __call__
self.load_middleware()
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/django-1.5/django/core/handlers/base.py", line 45, in load_middleware
for middleware_path in settings.MIDDLEWARE_CLASSES:
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/django-1.5/django/conf/__init__.py", line 53, in __getattr__
self._setup(name)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/django-1.5/django/conf/__init__.py", line 48, in _setup
self._wrapped = Settings(settings_module)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/django-1.5/django/conf/__init__.py", line 134, in __init__
raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e))
ImportError: Could not import settings 'settings' (Is it on sys.path?): No module named myfolder.settings
Traceback (most recent call last):
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 239, in Handle
handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/api/lib_config.py", line 353, in __getattr__
self._update_configs()
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/api/lib_config.py", line 289, in _update_configs
self._registry.initialize()
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/api/lib_config.py", line 164, in initialize
import_func(self._modname)
File "/base/data/home/apps/s~blue-myapp/app:master.375531077560785947/appengine_config.py", line 17, in
settings._target = None
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/django-1.5/django/utils/functional.py", line 227, in __setattr__
self._setup()
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/django-1.5/django/conf/__init__.py", line 48, in _setup
self._wrapped = Settings(settings_module)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/django-1.5/django/conf/__init__.py", line 134, in __init__
raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e))
ImportError: Could not import settings 'settings' (Is it on sys.path?): No module named myfolder.settings
This is our current appengine_config.py:
import sys
import logging
logging.debug(",\n".join(sys.path))
# Since Google App Engine's webapp framework uses Django templates, Django will half-initialize when webapp is loaded.
# This causes the initialization of the rest of Django's setting to be skipped. If you are getting this error, you need
# to explicitly force Django to reload your settings:
from django.conf import settings
settings._target = None
Logging sys.path from appengine_config.py does not change between a successful instance start and a failed instance start (apart from the XXXXXXXXXXX bit of course):
/base/data/home/apps/s~blue-persomi/app:master.3759720XXXXXXXXXXX,
/base/data/home/runtimes/python27/python27_dist/lib/python27.zip,
/base/data/home/runtimes/python27/python27_dist/lib/python2.7,
/base/data/home/runtimes/python27/python27_dist/lib/python2.7/plat-linux2,
/base/data/home/runtimes/python27/python27_dist/lib/python2.7/lib-tk,
/base/data/home/runtimes/python27/python27_dist/lib/python2.7/lib-old,
/base/data/home/runtimes/python27/python27_dist/lib/python2.7/lib-dynload,
/base/data/home/runtimes/python27/python27_dist/lib/python2.7/site-packages,
/base/data/home/runtimes/python27/python27_lib/versions/1,
/base/data/home/runtimes/python27/python27_lib/versions/third_party/MySQLdb-1.2.4b4,
/base/data/home/runtimes/python27/python27_lib/versions/third_party/django-1.5,
/base/data/home/runtimes/python27/python27_lib/versions/third_party/protorpc-1.0,
/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2,
/base/data/home/runtimes/python27/python27_lib/versions/third_party/webob-1.1.1,
/base/data/home/runtimes/python27/python27_lib/versions/third_party/yaml-3.10
It seems to be a path related issue as people have mentioned in your question's comments
Possible short-sighted solution add everything to your path manually - look at the top answer here:
How to import modules in Google App Engine?
At the very least, this will help narrow the problem to path related.
what the docs say: https://developers.google.com/appengine/docs/python/
The Python module include path includes your application's root
directory (the directory containing the app.yaml file). Modules you
create in your application's root directory are available using a path
from the root. Don't forget to create init.py files in
sub-directories, so Python will recognize the sub-directories as
packages.
so from what I can tell, b/c everything is at or below the app.yaml file in your question the path should already be correct.
double check all your __init__.py file files are in place and spelled correctly.
try deleting all of your *.pyc files and letting them be regenerated.
try importing from the container's folder name FOLDER_CONTAINING_YAML.myfolder.middleware
Add the following lines to your app.yaml
libraries:
- name: MySQLdb
version: "latest"
It is in the documentation here.
https://cloud.google.com/appengine/docs/python/cloud-sql/
Ideally it should have been documented in the GCM DJANGO guide. Would have saved me a lot of time.
Praveen

django could not import settings

I once wrote an django app, named superlists, and settings was in superlists.settings. It was for tutorial, and I was changing differnt settings, little realizing what I was doing.
And now when i starting new django project - it tells me it could not import superlists.settings
I reinstalled Pycharm, django, python (both versions 2.7 and 3.3), deleted all virtual envs, deleted everything that might be connected to it several times... IT IS STILL THERE! on a new, clean version of python, which even dont have django - there is DJANGO_SETTINGS_MODULE in sys.path pointing to superlists.settings. Each new project on a clean, with default settings, version of pycharm, tells me that it cannot import superlists.settings.
I delete DJANGO_SETTINGS_MODULE pointing to that settings from sys.path from cmd - exit then watch again - its there.
Where it takes it? I got no idea.
here is traceback:
Traceback (most recent call last):
File "D:/Python27/Lib/site-packages/django/bin/django-admin.py", line 5, in <module>
management.execute_from_command_line()
File "D:\Python27\Lib\site-packages\django\core\management\__init__.py", line 399, in execute_from_command_line
utility.execute()
File "D:\Python27\Lib\site-packages\django\core\management\__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "D:\Python27\Lib\site-packages\django\core\management\__init__.py", line 261, in fetch_command
commands = get_commands()
File "D:\Python27\Lib\site-packages\django\core\management\__init__.py", line 107, in get_commands
apps = settings.INSTALLED_APPS
File "D:\Python27\Lib\site-packages\django\conf\__init__.py", line 54, in __getattr__
self._setup(name)
File "D:\Python27\Lib\site-packages\django\conf\__init__.py", line 49, in _setup
self._wrapped = Settings(settings_module)
File "D:\Python27\Lib\site-packages\django\conf\__init__.py", line 132, in __init__
% (self.SETTINGS_MODULE, e)
ImportError: Could not import settings 'superlists\settings.py' (Is it on sys.path? Is there an import error in the settings file?): Import by filename is not supported.
It turned out that I created new environment system variable DJANGO_SETTINGS_MODULE. So evident, but it almost cost me sanity.
Change the environment variable from
DJANGO_SETTINGS_MODULE=superlist.superlist.settings
To DJANGO_SETTINGS_MODULE=superlist.settings
or use an __init__.py in superlist

Upgrading a Python Project from version 2.7 to 2.7.4

I have a Django "project" that I have inherited, which I am developing in Eclipse. On my OS (windows 7 32 bit), I have Python 2.7.4 installed, likewise for my virtualenv. However, on my project (extracted from SVN) the Python version is 2.7 only.
This causes a conflict when trying to create another superuser (I do not know the original superuser name/password) where I get the message:
cannot import maxrepeat
How do I upgrade the python version located at:
c:\users\"username"\workspace\"project"\scripts
from 2.7 to 2.7.4?
Apologies if I have omitted some important details, or if I am asking the wrong question as I am newbie to Django/python development.
EDIT
Having spoken to a friend before referring back to these responses (thanks btw), he advised me to copy over the contents of the 'scripts' folder within my virtualenv to the folder:
c:\users\"username"\workspace\"project"\scripts
I did that, so in theory, they are both now running from python version 2.7.4.
However, when I run the script
python manage.py createsuperuser
I get the following FULL Traceback:
Traceback (most recent call last):
File "manage.py", line 10, in
execute_from_command_line(sys.argv)
File "C:\users\alecc\workspace\hub\lib\site-packages\django\core\management\__init__.py"
, line 453, in execute_from_command_line
utility.execute()
File "C:\users\alecc\workspace\hub\lib\site-packages\django\core\management\__init__.py"
, line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\users\alecc\workspace\hub\lib\site-packages\django\core\management\__init__.py"
, line 263, in fetch_command
app_name = get_commands()[subcommand]
File "C:\users\alecc\workspace\hub\lib\site-packages\django\core\management\__init__.py"
, line 109, in get_commands
apps = settings.INSTALLED_APPS
File "C:\users\alecc\workspace\hub\lib\site-packages\django\conf\__init__.py", line 53,
in __getattr__
self._setup(name)
File "C:\users\alecc\workspace\hub\lib\site-packages\django\conf\__init__.py", line 48,
in _setup
self._wrapped = Settings(settings_module)
File "C:\users\alecc\workspace\hub\lib\site-packages\django\conf\__init__.py", line 134,
in __init__
raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SE
TTINGS_MODULE, e))
ImportError: Could not import settings 'hub.settings' (Is it on sys.path?): No module name
d hub.settings
I have checked the system variables and I'm sure my project is on the Python system path. I have also checked 'django.contrib.auth' is enabled in my INSTALLED_APPS in the settings.pyfile.
Edit 2
Many other posts suggest it's a cross over of Python versions. However when I check the version number using the command:
$scripts\python.exe --version
I get Python 2.7.4 for each installation (Project & virtualenv)
Based on this information:
File "C:\users\alecc\workspace\hub\lib\site-packages\django\conf\__init__.py", line 134,
in __init__
raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SE
TTINGS_MODULE, e))
ImportError: Could not import settings 'hub.settings' (Is it on sys.path?): No module name
d hub.settings
It looks like your application is called hub? The problem here is that it's trying to import hub.settings - but it can't find it. So for some reason, your settings.py is not on the path.
You can check this by editing C:\users\alecc\workspace\hub\lib\site-packages\django\conf\__init__.py, and somewhere before line 134 you can put import sys; print(sys.path). Then check to see that the path where your settings.py file lives is available. If it is, something else weird is happening.
If not, just go ahead and pull those lines from the __init__, and try running manage from the same directory your settings file is in.

Django manage.py custom command on Heroku

I am trying to run a cron job with a Heroku hosted website. I am using a Django back-end. I have written a custom manage.py command that works locally (status_crawl.py). I have seen a few guides and posts on how to fix this, but the suggestions did not work (such as Running Django custom manage.py task on Heroku - Importing Issues and this guide).
Even weirder is that it is recognizing the other commands. (And I managed to get the basic task to work by using runscript from the django-extensions framework). It is still bothering me though.
My basic file structure likes like:
likes:
...
management:
__init__.py
commands:
__init__.py
status_crawl.py
The traceback is:
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/__init__.py", line 453, in execute_from_command_line
utility.execute()
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/__init__.py", line 272, in fetch_command
klass = load_command_class(app_name, subcommand)
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/__init__.py", line 77, in load_command_class
module = import_module('%s.management.commands.%s' % (app_name, name))
File "/app/.heroku/python/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
__import__(name)
ImportError: No module named commands.status_crawl
The other weird thing is that when I run 'heroku run python manage.py help' the command is showing up as an option. It's just not running when I try to run the script myself using the manage.py command.
Thanks!
Probably it's related to your PYTHONPATH variable on Heroku. This variable on Heroku may differ from your local PYTHONPATH. Try to import from the root of your project, like:
from likes.management.commands import status_crawl

Categories