私は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 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 最初の 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'^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
URL とビューを編集しましたが、このエラーが発生しています。
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