Skip to main content
 首页 » 编程设计

python之使用 Tinymce 处理 django 错误。

2025年05月04日30Terrylee

我正在尝试在 django 中为 textarea 使用 TinyMCE 编辑器。我按照 django-tinymce.readthedocs.org/en/latest/usage.html#python-code 上给出的说明操作,这给了我一个错误

Environment: 
 
 
Request Method: GET 
Request URL: http://localhost:8000/home/ 
 
Django Version: 1.6.1 
Python Version: 2.7.3 
Installed Applications: 
('django.contrib.admin', 
 'django.contrib.auth', 
 'django.contrib.contenttypes', 
 'django.contrib.sessions', 
 'django.contrib.messages', 
 'django.contrib.staticfiles', 
 'tinymce') 
Installed Middleware: 
('django.contrib.sessions.middleware.SessionMiddleware', 
 'django.middleware.common.CommonMiddleware', 
 'django.middleware.csrf.CsrfViewMiddleware', 
 'django.contrib.auth.middleware.AuthenticationMiddleware', 
 'django.contrib.messages.middleware.MessageMiddleware', 
 'django.middleware.clickjacking.XFrameOptionsMiddleware') 
 
 
Traceback: 
File "/usr/local/lib/python2.7/dist-packages/Django-1.6.1-py2.7.egg/django/core/handlers/base.py" in get_response 
  114.                     response = wrapped_callback(request, *callback_args, **callback_kwargs) 
File "/home/hussain/django/ali/ali/views.py" in HomePage 
  6.     print " form :: ",form 
File "/usr/local/lib/python2.7/dist-packages/Django-1.6.1-py2.7.egg/django/utils/encoding.py" in <lambda> 
  60.         klass.__str__ = lambda self: self.__unicode__().encode('utf-8') 
File "/usr/local/lib/python2.7/dist-packages/Django-1.6.1-py2.7.egg/django/forms/forms.py" in __str__ 
  103.         return self.as_table() 
File "/usr/local/lib/python2.7/dist-packages/Django-1.6.1-py2.7.egg/django/forms/forms.py" in as_table 
  223.             errors_on_separate_row = False) 
File "/usr/local/lib/python2.7/dist-packages/Django-1.6.1-py2.7.egg/django/forms/forms.py" in _html_output 
  186.                     'field': six.text_type(bf), 
File "/usr/local/lib/python2.7/dist-packages/Django-1.6.1-py2.7.egg/django/forms/forms.py" in __str__ 
  425.         return self.as_widget() 
File "/usr/local/lib/python2.7/dist-packages/Django-1.6.1-py2.7.egg/django/forms/forms.py" in as_widget 
  475.         return widget.render(name, self.value(), attrs=attrs) 
File "/usr/local/lib/python2.7/dist-packages/Django-1.6.1-py2.7.egg/django/forms/widgets.py" in render 
  572.         options = self.render_options(choices, value) 
File "/usr/local/lib/python2.7/dist-packages/Django-1.6.1-py2.7.egg/django/forms/widgets.py" in render_options 
  528.         for option_value, option_label in chain(self.choices, choices): 
File "/usr/local/lib/python2.7/dist-packages/Django-1.6.1-py2.7.egg/django/forms/models.py" in __iter__ 
  1044.             for obj in self.queryset.all(): 
File "/usr/local/lib/python2.7/dist-packages/Django-1.6.1-py2.7.egg/django/db/models/query.py" in __iter__ 
  96.         self._fetch_all() 
File "/usr/local/lib/python2.7/dist-packages/Django-1.6.1-py2.7.egg/django/db/models/query.py" in _fetch_all 
  854.             self._result_cache = list(self.iterator()) 
File "/usr/local/lib/python2.7/dist-packages/Django-1.6.1-py2.7.egg/django/db/models/query.py" in iterator 
  220.         for row in compiler.results_iter(): 
File "/usr/local/lib/python2.7/dist-packages/Django-1.6.1-py2.7.egg/django/db/models/sql/compiler.py" in results_iter 
  710.         for rows in self.execute_sql(MULTI): 
File "/usr/local/lib/python2.7/dist-packages/Django-1.6.1-py2.7.egg/django/db/models/sql/compiler.py" in execute_sql 
  781.         cursor.execute(sql, params) 
File "/usr/local/lib/python2.7/dist-packages/Django-1.6.1-py2.7.egg/django/db/backends/util.py" in execute 
  69.             return super(CursorDebugWrapper, self).execute(sql, params) 
File "/usr/local/lib/python2.7/dist-packages/Django-1.6.1-py2.7.egg/django/db/backends/util.py" in execute 
  53.                 return self.cursor.execute(sql, params) 
File "/usr/local/lib/python2.7/dist-packages/Django-1.6.1-py2.7.egg/django/db/utils.py" in __exit__ 
  99.                 six.reraise(dj_exc_type, dj_exc_value, traceback) 
File "/usr/local/lib/python2.7/dist-packages/Django-1.6.1-py2.7.egg/django/db/backends/util.py" in execute 
  53.                 return self.cursor.execute(sql, params) 
File "/usr/local/lib/python2.7/dist-packages/Django-1.6.1-py2.7.egg/django/db/backends/sqlite3/base.py" in execute 
  450.         return Database.Cursor.execute(self, query, params) 
 
Exception Type: OperationalError at /home/ 
Exception Value: no such table: django_site 

我觉得不明白这个很愚蠢。 :'(.

这些是我的文件。

settings.py

""" 
Django settings for ali project. 
 
For more information on this file, see 
https://docs.djangoproject.com/en/1.6/topics/settings/ 
 
For the full list of settings and their values, see 
https://docs.djangoproject.com/en/1.6/ref/settings/ 
""" 
 
# Build paths inside the project like this: os.path.join(BASE_DIR, ...) 
import os 
BASE_DIR = os.path.dirname(os.path.dirname(__file__)) 
 
# Django settings for corating project. 
 
PROJECT_DIR = os.path.dirname(__file__) 
 
print 'BASE_DIR ',BASE_DIR 
print 'PROJECT_DIR ',PROJECT_DIR 
# Quick-start development settings - unsuitable for production 
# See https://docs.djangoproject.com/en/1.6/howto/deployment/checklist/ 
 
# SECURITY WARNING: keep the secret key used in production secret! 
SECRET_KEY = '-=153zwy^8$sz%gsw#kb377pp@r3wmcer(4tc$j-h4(^3s+zw+' 
 
# SECURITY WARNING: don't run with debug turned on in production! 
DEBUG = True 
 
TEMPLATE_DEBUG = True 
 
ALLOWED_HOSTS = [] 
 
 
 
 
# Application definition 
 
INSTALLED_APPS = ( 
    'django.contrib.admin', 
    'django.contrib.auth', 
    'django.contrib.contenttypes', 
    'django.contrib.sessions', 
    'django.contrib.messages', 
    'django.contrib.staticfiles', 
    'tinymce', 
) 
 
MIDDLEWARE_CLASSES = ( 
    'django.contrib.sessions.middleware.SessionMiddleware', 
    'django.middleware.common.CommonMiddleware', 
    'django.middleware.csrf.CsrfViewMiddleware', 
    'django.contrib.auth.middleware.AuthenticationMiddleware', 
    'django.contrib.messages.middleware.MessageMiddleware', 
    'django.middleware.clickjacking.XFrameOptionsMiddleware', 
) 
 
ROOT_URLCONF = 'ali.urls' 
 
WSGI_APPLICATION = 'ali.wsgi.application' 
 
 
# Database 
# https://docs.djangoproject.com/en/1.6/ref/settings/#databases 
 
DATABASES = { 
    'default': { 
        'ENGINE': 'django.db.backends.sqlite3', 
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), 
    } 
} 
 
# Internationalization 
# https://docs.djangoproject.com/en/1.6/topics/i18n/ 
 
LANGUAGE_CODE = 'en-us' 
 
TIME_ZONE = 'UTC' 
 
USE_I18N = True 
 
USE_L10N = True 
 
USE_TZ = True 
 
 
# Static files (CSS, JavaScript, Images) 
# https://docs.djangoproject.com/en/1.6/howto/static-files/ 
 
 
STATIC_ROOT = os.path.join(PROJECT_DIR, 'static_root') 
# URL prefix for static files. 
# Example: "http://example.com/static/", "http://static.example.com/" 
STATIC_URL = '/static/' 
 
# Additional locations of static files 
STATICFILES_DIRS = ( 
    os.path.join(PROJECT_DIR, 'static'), 
    # Put strings here, like "/home/html/static" or "C:/www/django/static". 
    # Always use forward slashes, even on Windows. 
    # Don't forget to use absolute paths, not relative paths. 
) 
 
TINYMCE_JS_URL = os.path.join(STATIC_URL, "js/tiny_mce/tiny_mce.js") 
TINYMCE_JS_ROOT = os.path.join(STATIC_URL, "js/tiny_mce") 
 
print "TINYMCE_JS_ROOT  ",TINYMCE_JS_ROOT 
print "TINYMCE_JS_URL   ",TINYMCE_JS_URL 
 
 
TEMPLATE_DIRS = ( 
    '/home/hussain/django/ali/ali/templates/', 
    # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates". 
    # Always use forward slashes, even on Windows. 
    # Don't forget to use absolute paths, not relative paths. 
) 

表单.py

from django import forms 
from django.contrib.flatpages.models import FlatPage 
from tinymce.widgets import TinyMCE 
 
 
class FlatPageForm(forms.ModelForm): 
    content = forms.CharField(widget=TinyMCE(attrs={'cols': 80, 'rows': 30})) 
 
    class Meta: 
        model = FlatPage 

views.py

from django.shortcuts import render_to_response 
from page.forms import FlatPageForm  
 
def HomePage(request): 
    form = FlatPageForm() 
    print " form :: ",form 
    return render_to_response('HomePage.html',{'form':form}) 

urls.py

from django.conf.urls import patterns, include, url 
from ali.views import HomePage 
from django.contrib import admin 
admin.autodiscover() 
 
urlpatterns = patterns('', 
    # Examples: 
    # url(r'^$', 'ali.views.home', name='home'), 
    # url(r'^blog/', include('blog.urls')), 
 
    #url(r'^admin/', include(admin.site.urls)), 
    url(r'^home/', 'ali.views.HomePage' ), 
    url(r'^tinymce/', include('tinymce.urls') ), 
 
) 

请您参考如下方法:

您似乎没有安装 flatpages 应用程序。 flatpages 应用程序也依赖于 sites framework按照 documentation 中的步骤进行操作做对了:

--- 摘录 --

要安装平面应用程序,请按照下列步骤操作:

  1. 通过将“django.contrib.sites”添加到您的 INSTALLED_APPS 设置来安装站点框架(如果它还没有在其中的话)。

  2. 同时确保您已将 SITE_ID 正确设置为设置文件所代表的站点的 ID。这通常为 1(即 SITE_ID = 1,但如果您使用站点框架来管理多个站点,则它可能是不同站点的 ID。