2013-06-16 5 views
6

Djangoチュートリアルの作業中、特に投票アプリケーションにビューを追加する際に問題が発生しました。参考までに、これは私をトリップセクションの始まりである:その前のセクションにhttps://docs.djangoproject.com/en/1.5/intro/tutorial03/#writing-more-viewsDjangoチュートリアルのパート3でこのエラーの原因は何ですか?

、私は世論調査/ビューが無い問題に現れるように得ることができます。しかし、私は世論調査/ views.pyこのようなに見えるようにするために、3つの追加のビューを追加するとき:

def detail(request, poll_id): 
    return HttpResponse("You're looking at poll %s." % poll_id) 

def results(request, poll_id): 
    return HttpResponse("You're looking at the results of poll %s." % poll_id) 

def vote(request, poll_id): 
    return HttpResponse("You're voting on poll %s." % poll_id) 

、その後ポーリングを作る/ urls.pyこのような見て:私が得る

from django.conf.urls import patterns, url 

from polls import views 

urlpatterns = patterns('', 
    # ex: /polls/ 
    url(r'^$', views.index, name='index'), 
    # ex: /polls/5/ 
    url(r'^(?P<poll_id>\d+)/$', views.detail, name='detail'), 
    # ex: /polls/5/results/ 
    url(r'^(?P<poll_id>\d+)/results/$', views.results, name='results'), 
    # ex: /polls/5/vote/ 
    url(r'^(?P<poll_id>\d+)/vote/$', views.vote, name='vote'), 
) 

エラー。さておき、私の個人用サイトROOT_URLCONFとしてはこのようになります個人用サイト/ urls.pyを指している:

from django.conf.urls import patterns, include, url 

from django.contrib import admin 
admin.autodiscover() 

urlpatterns = patterns('', 
    url(r'^polls/', include('polls.urls')), 
    url(r'^admin/', include(admin.site.urls)), 
) 

私は取得していますエラーは次のとおりです。次も

Environment: 


Request Method: GET 
Request URL: http://127.0.0.1:8000/polls/34 

Django Version: 1.5.1 
Python Version: 2.7.3 
Installed Applications: 
('django.contrib.auth', 
'django.contrib.contenttypes', 
'django.contrib.sessions', 
'django.contrib.sites', 
'django.contrib.messages', 
'django.contrib.staticfiles', 
'django.contrib.admin', 
'polls') 
Installed Middleware: 
('django.middleware.common.CommonMiddleware', 
'django.contrib.sessions.middleware.SessionMiddleware', 
'django.middleware.csrf.CsrfViewMiddleware', 
'django.contrib.auth.middleware.AuthenticationMiddleware', 
'django.contrib.messages.middleware.MessageMiddleware') 


Traceback: 
File "/home/matthew/.virtualenvs/djangopoll/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response 
    92.      response = middleware_method(request) 
File "/home/matthew/.virtualenvs/djangopoll/local/lib/python2.7/site-packages/django/middleware/common.py" in process_request 
    69.    if (not urlresolvers.is_valid_path(request.path_info, urlconf) and 
File "/home/matthew/.virtualenvs/djangopoll/local/lib/python2.7/site-packages/django/core/urlresolvers.py" in is_valid_path 
    551.   resolve(path, urlconf) 
File "/home/matthew/.virtualenvs/djangopoll/local/lib/python2.7/site-packages/django/core/urlresolvers.py" in resolve 
    440.  return get_resolver(urlconf).resolve(path) 
File "/home/matthew/.virtualenvs/djangopoll/local/lib/python2.7/site-packages/django/core/urlresolvers.py" in resolve 
    319.    for pattern in self.url_patterns: 
File "/home/matthew/.virtualenvs/djangopoll/local/lib/python2.7/site-packages/django/core/urlresolvers.py" in url_patterns 
    347.   patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module) 
File "/home/matthew/.virtualenvs/djangopoll/local/lib/python2.7/site-packages/django/core/urlresolvers.py" in urlconf_module 
    342.    self._urlconf_module = import_module(self.urlconf_name) 
File "/home/matthew/.virtualenvs/djangopoll/local/lib/python2.7/site-packages/django/utils/importlib.py" in import_module 
    35.  __import__(name) 
File "/home/matthew/djangopoll/mysite/mysite/urls.py" in <module> 
    8.      url(r'^polls/', include('polls.urls')), 
File "/home/matthew/.virtualenvs/djangopoll/local/lib/python2.7/site-packages/django/conf/urls/__init__.py" in include 
    25.   urlconf_module = import_module(urlconf_module) 
File "/home/matthew/.virtualenvs/djangopoll/local/lib/python2.7/site-packages/django/utils/importlib.py" in import_module 
    35.  __import__(name) 

Exception Type: SyntaxError at /polls/34 
Exception Value: invalid syntax (urls.py, line 9) 

、有益なことがあります

Pを使用してDjangoの1.5
  • を使用して
    • ython 2.7.3
    • VIRTUALENV

    使用してVirtualBoxの

  • を使用してDjangoの1.5ドキュメンテーション
  • を使用して、任意のヘルプは歓迎です!なぜこのエラーが発生するのですか?

  • +1

    'polls.urls'をDjangoシェルからインポートできますか? –

    答えて

    7

    Pythonがpolls.urlsをインポートできないように見えるため、__import__(name)が失敗します。ここでの「名前」は、モジュール名「polls.urls」です。

    システムがpolls.urlsをインポートできない理由を調べるには、インタラクティブにそれをインポートしてみてください。

    $ python manage.py shell 
    
    Python ... blah blah 
    ... 
    
    > import polls.urls 
    

    これは失敗しますが、トレースバックを使用すると、エラーの原因となる次の手がかりが得られます。

    幸運を祈る!

    +2

    インタラクティブなDjangoシェルの使い方を学んだので、これは役に立ちました...しかし、私が持っていた問題は信じられないほどばかげたものでした。私が追加したビューの前に、最初のビューの後にカンマが必要であることが分かりました。ドー! – Matt

    +1

    誰もが間違いを犯します。あなたは進歩を遂げることについて気分がいいはずです! – orlenko

    +0

    これはまた、対話シェルを使用して、どのurls.py djangoが読み込みに問題があるかを把握するのに役立ちました。私のエラーは同じように再結婚しました。 –

    関連する問題