【问题标题】:django cms apphook errordjango cms apphook 错误
【发布时间】:2011-07-26 21:43:06
【问题描述】:

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

没有名为 urls 的模块 .

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

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

我的 cms_app.py 和教程中给出的完全一样。

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

python django-admin.py 启动项目 我的项目

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

python manage.py 先启动app

现在插件运行良好,在尝试使用 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

我的项目文件夹中有一个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

【问题讨论】:

    标签: django url django-cms


    【解决方案1】:

    您没有包含“第一个”应用的 URL 的 first.urls 模块。在您的文件 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")
    

    【讨论】:

    • 感谢您的回复,您能告诉我应该使用哪种 URL 模式吗?!抱歉,现阶段我真的很困惑。
    • 为此,我需要了解有关您应用中的视图的更多信息。
    • 我的视图文件中现在没有任何内容。它是空的。
    • 在这种情况下,有一个 apphook 是没有意义的。 apphooks 背后的原因是当您希望将应用中的视图挂钩到特定页面(及其下方)时。
    • 我已经在第一个应用文件夹中添加了视图。我已经编辑了我的问题。
    猜你喜欢
    • 2012-03-30
    • 2019-01-14
    • 2011-07-26
    • 2013-05-09
    • 2015-09-04
    • 2012-03-28
    • 2017-11-15
    • 2015-10-03
    • 1970-01-01
    相关资源
    最近更新 更多