Django和TinyMCE:NameError:name' url'未定义

时间:2018-06-01 04:48:47

标签: python django tinymce

我想让TinyMCE在Django中工作。这是我做的:

  • 使用此程序包作为参考:django-tinymce4-lite
  • 成功运行pip install django-tinymce4-lite;包安装正常
  • 在settings.py
  • 中为INSTALLED_APPS添加了tinymce

然后在这里变得棘手:

Add tinymce.urls to urls.py for your project:

urlpatterns = [
    ...
    url(r'^tinymce/', include('tinymce.urls')),
    ...
]

当我这样做时,我收到此错误:

url(r'^tinymce/', include('tinymce.urls')),  
NameError: name 'url' is not defined

我尝试了以下内容:

  • 重启django
  • 我没有把它放在我的项目的urls.py中,而是尝试了我的应用程序的urls.py
  • 我尝试将此转换为" 路径(' tinymce /',include(' tinymce.urls')),& #34;因为所有其他条目都使用' path'而不是' url',但那也没有用(ModuleNotFoundError:没有名为' tinymce.urls的模块)
  • 我试过another tinymce plugin

这些都没有帮助。有什么建议?

更新

根据建议,我将url更新为路径。现在我有一个新错误:

ModuleNotFoundError: No module named 'tinymce.urls'

这是我的urls.py:

from django.urls import include, path
from django.contrib import admin

from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    path('', include('core.urls')),
    path('tinymce/', include('tinymce.urls')),
]

if settings.DEBUG:
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

这个错误让我怀疑我是否正确安装了该插件。但似乎我有:

pip install django-tinymce4-lite
Requirement already satisfied: django-tinymce4-lite in /usr/local/lib/python3.6/site-packages
Requirement already satisfied: Django>=1.8.0 in /usr/local/lib/python3.6/site-packages (from django-tinymce4-lite)
Requirement already satisfied: jsmin in /usr/local/lib/python3.6/site-packages (from django-tinymce4-lite)
Requirement already satisfied: pytz in /usr/local/lib/python3.6/site-packages (from Django>=1.8.0->django-tinymce4-lite)

3 个答案:

答案 0 :(得分:1)

由于您使用的是django 2.0,因此应使用path代替url

from django.urls import path

urlpatterns = [
    ...
    path('tinymce/', include('tinymce.urls')),
    ...
]

您可以找到更多详情here

答案 1 :(得分:0)

您有NameError,因为您在url中引用urls.py但尚未导入它。将以下行添加到urls.py

from django.conf.urls import url

答案 2 :(得分:0)

是因为他正在使用django 2 根据您可以使用的documentation path and re_path示例:

from django.urls import path , re_path

urlpatterns = [
     path('tinymce/', include('tinymce.urls')),

    # or
    #path('tinymce/', include('tinymce.urls')),

]