Django可以'不能在设置中导入ckeditor,但可以在shell中导入

Django can't import ckeditor in settings but can in shell?

本文关键字:导入 ckeditor 但可以 shell 可以 不能 Django 设置      更新时间:2023-09-26

CKeditor由于某些原因在从设置加载时找不到,但从python shell导入时效果良好。

Django 1.7。Python(2.7.9)是一个替代安装。通过pip2.7 install django-ckeditor 安装的ckeditor

https://github.com/django-ckeditor/django-ckeditor

设置中没有 ckeditor。py:

[user@server mysite]$ python2.7 manage.py shell
Python 2.7.9 (default, Feb  1 2015, 21:31:28) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> import ckeditor
>>> 
>>> 
>>> 
[user@server mysite]$ python2.7 manage.py collectstatic
You have requested to collect static files at the destination
location as specified in your settings:
    /var/www/mysite/static
This will overwrite existing files!
Are you sure you want to do this?
Type 'yes' to continue, or 'no' to cancel: yes
0 static files copied to '/var/www/mysite/static', 2357 unmodified.

将'ckeditor'添加到settings.py:

INSTALLED_APPS = (
    'ckeditor ',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'allauth',
    'documentation',
    'allauth.account',
    'allauth.socialaccount',
    'django.contrib.auth',
    'app',
    'thunderdome',
    'suit',
    # Uncomment the next line to enable the admin:
    'django.contrib.admin',
    # Uncomment the next line to enable admin documentation:
    'django.contrib.admindocs',
   # 'supplementtut',
    'dbmail',
    'tinymce', 
)
CKEDITOR_UPLOAD_PATH = "uploads/"

Shell、collectstatic、runserver都一样:

[user@server mysite]$ python2.7 manage.py collectstatic
Traceback (most recent call last):
  File "manage.py", line 17, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 354, in execute
    django.setup()
  File "/usr/local/lib/python2.7/site-packages/django/__init__.py", line 21, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/usr/local/lib/python2.7/site-packages/django/apps/registry.py", line 85, in populate
    app_config = AppConfig.create(entry)
  File "/usr/local/lib/python2.7/site-packages/django/apps/config.py", line 87, in create
    module = import_module(entry)
  File "/usr/local/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
ImportError: No module named ckeditor 
[user@server mysite]$ 

已验证已安装:

[user@server mysite]$ cd /usr/local/lib/python2.7/site-packages/ckeditor
[user@server ckeditor]$ ls -l
total 92
-rw-r--r-- 1 user domain users 1371 Apr  6 13:03 fields.py
-rw-r--r-- 1 user domain users 1975 Apr  6 13:03 fields.pyc
drwxr-xr-x 2 user domain users 4096 Apr  6 13:03 image
-rw-r--r-- 1 user domain users  297 Apr  6 13:03 image_processing.py
-rw-r--r-- 1 user domain users  611 Apr  6 13:03 image_processing.pyc
-rw-r--r-- 1 user domain users  721 Apr  6 13:03 __init__.py
-rw-r--r-- 1 user domain users  853 Apr  6 13:03 __init__.pyc
drwxr-xr-x 3 user domain users 4096 Apr  6 13:03 management
-rw-r--r-- 1 user domain users    0 Apr  6 13:03 models.py
-rw-r--r-- 1 user domain users  144 Apr  6 13:03 models.pyc
drwxr-xr-x 3 user domain users 4096 Apr  6 13:03 static
drwxr-xr-x 3 user domain users 4096 Apr  6 13:03 templates
-rw-r--r-- 1 user domain users  411 Apr  6 13:03 urls.py
-rw-r--r-- 1 user domain users  649 Apr  6 13:03 urls.pyc
-rw-r--r-- 1 user domain users 1025 Apr  6 13:03 utils.py
-rw-r--r-- 1 user domain users 2207 Apr  6 13:03 utils.pyc
-rw-r--r-- 1 user domain users 4749 Apr  6 13:03 views.py
-rw-r--r-- 1 user domain users 5089 Apr  6 13:03 views.pyc
-rw-r--r-- 1 user domain users 4583 Apr  6 13:03 widgets.py
-rw-r--r-- 1 user domain users 4957 Apr  6 13:03 widgets.pyc
[user@server ckeditor]$ 

更新:已检查以验证是否已安装:

皮普冻结:

[user@server mysite]$ pip2.7 freeze | grep ckeditor
django-ckeditor==4.4.7

您的INSTALLED_APPS中有一个尾随空格:请尝试'ckeditor'而不是'ckeditor '