django cms apphook错误

时间:2011-03-31 13:14:00

标签: django url django-cms

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

  

没有名为urls的模块   。

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

设置网址有问题,还是需要在我的应用目录中创建新的urls.py文件?

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

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

  

python django-admin.py startproject   myproject的

在参考了为cms提供的教程后,我使用基本命令

创建了一个名为first的插件
  

python manage.py startapp first

现在插件工作得很好,在尝试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

1 个答案:

答案 0 :(得分:1)

您没有first.urls模块,其中包含“首个”应用的网址。在您的文件first/models.py旁边,创建一个文件first/urls.py,其中包含“首个”应用的网址格式。

对于您在问题中提供的观点,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")