1

我正在学习 django-cms。我尝试制作非常成功的自定义插件,但是当我尝试将我的自定义插件挂钩到 apphook 时,它给了我一个错误,说,

没有名为 urls 的模块。

我按照 django cms 站点文档中给出的教程进行操作,并创建了 cms_app.py 文件。目前,我的应用程序目录包含为 django cms 制作自定义插件所需的所有文件,以及 cms_app.py 的附加文件。

url 的设置有问题还是我需要在我的 app 目录中创建一个新的 urls.py 文件?

我的 cms_app.py 与教程中给出的完全相同。

我使用命令创建了一个名为 myproject 的项目 -

python django-admin.py startproject myproject

在参考为 cms 提供的教程后,我使用基本命令创建了一个名为 first 的插件

首先 python manage.py startapp

现在插件运行良好,在尝试使用 apphook 之前的目录结构是,

first/
    __init__.py
    cms_plugins.py
    models.py
    tests.py
    views.py

现在尝试在 apphook 中挂钩应用程序后,目录结构为:

first/
    __init__.py
    cms_app.py
    cms_plugins.py
    models.py
    tests.py
    views.py

我的 cms_app.py 如下:

from cms.app_base import CMSApp
from cms.apphook_pool import apphook_pool
from django.utils.translation import ugettext_lazy as _

class FirstApp(CMSApp):
    name = _("First App") # give your app a name, this is required
    urls = ["first.urls"] # link your app to url configuration(s)

apphook_pool.register(FirstApp) # register your app

我在 myproject 文件夹中有一个 urls.py 文件,内容如下:

from django.conf.urls.defaults import *
from django.contrib import admin
from django.conf import settings


# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'myproject.views.home', name='home'),
    # url(r'^myproject/', include('myproject.foo.urls')),

    # Uncomment the admin/doc line below to enable admin documentation:
    # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

    # Uncomment the next line to enable the admin:
        url(r'^admin/', include(admin.site.urls)),
    url(r'^', include('cms.urls')),

)

if settings.DEBUG:
    urlpatterns = patterns('',
        (r'^' + settings.MEDIA_URL.lstrip('/'), include('appmedia.urls')),
    ) + urlpatterns

我已经按照教程中的说明重新启动了服务器,但没有成功。关于我的简单应用程序有什么问题的任何想法?!

编辑 - 1 我的意见文件如下:

from django.http import HttpResponse

def index(request):
    “””Generate the context for the main summary page”””
    return render_to_response(‘first/first.html’)

编辑 - 2 我已将第一个应用程序文件夹中的 urls.py 更改为:

from django.conf.urls.defaults import *
from django.contrib import admin
from django.conf import settings


# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'myproject.views.home', name='home'),
    # url(r'^myproject/', include('myproject.foo.urls')),

    # Uncomment the admin/doc line below to enable admin documentation:
    # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

    # Uncomment the next line to enable the admin:
        #url(r'^admin/', include(admin.site.urls)),
    url(r'^first/$', include('first.views.index')),

)

if settings.DEBUG:
    urlpatterns = patterns('',
        (r'^' + settings.MEDIA_URL.lstrip('/'), include('appmedia.urls')),
    ) + urlpatterns

但是现在我收到了这个错误:

SyntaxError at /

Non-ASCII character '\xe2' in file /home/naveen/django_projects/myproject/first/views.py on line 4, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details (views.py, line 4)

Request Method:     GET
Request URL:    http://localhost:8000/
Django Version:     1.3
Exception Type:     SyntaxError
Exception Value:    

Non-ASCII character '\xe2' in file /home/naveen/django_projects/myproject/first/views.py on line 4, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details (views.py, line 4)

Exception Location:     /usr/local/lib/python2.6/dist-packages/django/utils/importlib.py in import_module, line 35
Python Executable:  /usr/bin/python
Python Version:     2.6.6
Python Path:    

['/home/naveen/django_projects/myproject',
 '/usr/local/lib/python2.6/dist-packages/pip-0.8.3-py2.6.egg',
 '/usr/local/lib/python2.6/dist-packages',
 '/usr/local/lib/python2.6/dist-packages/django_evolution-0.6.2-py2.6.egg',
 '/usr/lib/python2.6',
 '/usr/lib/python2.6/plat-linux2',
 '/usr/lib/python2.6/lib-tk',
 '/usr/lib/python2.6/lib-old',
 '/usr/lib/python2.6/lib-dynload',
 '/usr/local/lib/python2.6/dist-packages',
 '/usr/lib/python2.6/dist-packages',
 '/usr/lib/python2.6/dist-packages/PIL',
 '/usr/lib/python2.6/dist-packages/gst-0.10',
 '/usr/lib/pymodules/python2.6',
 '/usr/lib/python2.6/dist-packages/gtk-2.0',
 '/usr/lib/pymodules/python2.6/gtk-2.0']

Server time:    Thu, 31 Mar 2011 11:00:41 -0500

我已经编辑了网址和视图,但现在我收到了这个错误。

NameError at /first/

global name 'render_to_response' is not defined

Request Method:     GET
Request URL:    http://localhost:8000/first/?preview
Django Version:     1.3
Exception Type:     NameError
Exception Value:    

global name 'render_to_response' is not defined

Exception Location:     /home/naveen/django_projects/myproject/first/views.py in index, line 5
Python Executable:  /usr/bin/python
Python Version:     2.6.6
Python Path:    

['/home/naveen/django_projects/myproject',
 '/usr/local/lib/python2.6/dist-packages/pip-0.8.3-py2.6.egg',
 '/usr/local/lib/python2.6/dist-packages',
 '/usr/local/lib/python2.6/dist-packages/django_evolution-0.6.2-py2.6.egg',
 '/usr/lib/python2.6',
 '/usr/lib/python2.6/plat-linux2',
 '/usr/lib/python2.6/lib-tk',
 '/usr/lib/python2.6/lib-old',
 '/usr/lib/python2.6/lib-dynload',
 '/usr/local/lib/python2.6/dist-packages',
 '/usr/lib/python2.6/dist-packages',
 '/usr/lib/python2.6/dist-packages/PIL',
 '/usr/lib/python2.6/dist-packages/gst-0.10',
 '/usr/lib/pymodules/python2.6',
 '/usr/lib/python2.6/dist-packages/gtk-2.0',
 '/usr/lib/pymodules/python2.6/gtk-2.0']

Server time:    Thu, 31 Mar 2011 14:50:32 -0500
4

1 回答 1

1

您没有包含first.urls“第一个”应用程序的 URL 的模块。在您的文件旁边first/models.py,创建一个first/urls.py包含“第一个”应用程序的 URL 模式的文件。

对于您在问题中给出的观点,urls.py 应如下所示:

from django.conf.urls.defaults import *
from first.views import index

urlpatterns = patterns('',
    url(r'^$', index),
)

另请注意,在您的视图中,您使用非标准引号字符,它应该如下所示:

from django.http import HttpResponse

def index(request):
    """Generate the context for the main summary page"""
    return render_to_response("first/first.html")
于 2011-03-31T13:16:39.337 回答