2011-03-31 56 views
1

我在学习django-cms。我试图使这是相当成功的自定义插件,但是当我试图钩我的定制插件来apphook,它给我的错误,他说,django cms apphook错误

No Module named urls .

我跟着这是在Django的CMS网站文档给出的教程,并创建了cms_app.py文件。目前,我的应用程序目录包含为django cms创建自定义插件所需的所有文件,以及一个额外的cms_app.py文件。

设置网址时出现错误或者我需要在我的应用目录中创建一个新的urls.py文件?

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

我已创建使用命令调用MyProject的项目 -

python django-admin.py startproject myproject

指为CMS提供的教程后,我创建了第一个叫的插件,使用基本的命令

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

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

对于您在问题中给出的观点,网址。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") 
+0

感谢回答,你能告诉我是什么网址模式应该在那里?!对不起,我现在很困惑。 – Maverick 2011-03-31 13:18:42

+0

因为我需要了解更多关于您的应用程序中的意见。 – ojii 2011-03-31 13:39:37

+0

现在我的视图文件中没有任何内容。它是空的。 – Maverick 2011-03-31 13:44:44