2012-04-12 59 views
1

我有一個在生產服務器上運行的Django應用程序。它在nginx後面用gunicorn 0.14.2處理。當我重新加載應用程序(通過重新加載gunicorn工人),我得到這個錯誤:爲什麼Django在加載我的URLconf時發出警告?

Traceback (most recent call last): 

File "/opt/nybooks/venv/myapp/lib/python2.6/site-packages/django/core/handlers/base.py", line 101, in get_response 
    request.path_info) 

File "/opt/nybooks/venv/myapp/lib/python2.6/site-packages/django/core/urlresolvers.py", line 250, in resolve 
    for pattern in self.url_patterns: 

File "/opt/nybooks/venv/myapp/lib/python2.6/site-packages/django/core/urlresolvers.py", line 283, in _get_url_patterns 
    raise ImproperlyConfigured("The included urlconf %s doesn't have any patterns in it" % self.urlconf_name) 

ImproperlyConfigured: The included urlconf myapp.urls doesn't have any patterns in it 

其他同這一問題已經普遍注意到,雖然在URL配置使用reverse發生了,但我沒有在任何使用reverse URLconfs(也不用於任何第三方應用程序)。此外,這個錯誤只有發生在生產中 - 從未在開發中(使用Django dev服務器)或在我的分段服務器上(使用nginx後面的gunicorn 0.14.2)。在重新加載期間,它在任何其他時間似乎也不會造成該網站的麻煩。

任何想法是什麼原因造成的問題?


下面是主要的URLconf(和一個堆棧跟蹤參考):

from django.conf.urls.defaults import * 
from django.contrib import admin 
from django.conf import settings 
from django.http import HttpResponse, Http404 
from django.views.generic.simple import direct_to_template, redirect_to 

from myapp.apps.magazine.views import * 
from myapp.apps.books.views import * 
from myapp.apps.forms.views import * 
from myapp.apps.blogext.views import * 
from myapp.apps.sharing.views import expand_url, email_link_send 
from myapp.apps.magazine.feeds import * 

from satchmo_utils import urlhelper 

from satchmo_store.urls import urlpatterns 
from myapp.apps.myapp_shop.views import * 

admin.autodiscover() 

if settings.SHOP_BASE == '': 
    shopregex = '^' 
else: 
    shopregex = '^' + settings.SHOP_BASE[1:] + '/' 


myapp_patterns = patterns('', 
    # calendar 
    (r'^calendar/',   include('events.urls')), 
    # for multimedia SWF access 
    #(r'^crossdomain.xml$', direct_to_template, {'template': 'crossdomain.xml'}), 
    # intercept checkout 
    # TODO: need to use a config value 
    (r'^catalog/checkout/$', 'myapp.apps.cart.views.myapp_checkout', {'SSL': not settings.LOCAL_DEV}, 'myapp_checkout'), 
    (r'^catalog/add/$', 'myapp.apps.cart.views.smart_add_wrapper', {}, 'myapp_smart_add'), 
    # URLs for NYRB apps 
    (r'^$',       direct_to_template, {'template': 'newhomepage.html'}), 
    (r'^newhomepage/$',    direct_to_template, {'template': 'newhomepage.html'}), 
    (r'^mobile/$',     redirect_to, {'url': '/', 'permanent': True}), 
    (r'^books/authors/',   include('myapp.apps.books.urls.authors')), 
    (r'^books/',     include('myapp.apps.books.urls.books')), 
    (r'^articles/',     include('myapp.apps.magazine.urls.articles')), 
    (
    r'^mobile/articles/archives/(?P<year>\d{4})/(?P<month>[a-z]{3})/(?P<day>\d{2})/(?P<slug>[-\w]+)/$', 
    redirect_to, 
    {'url': '/articles/archives/%(year)s/%(month)s/%(day)s/%(slug)s/', 'permanent': True}, 
), 
    # for national poetry month (april) 
    url(
    regex = r'^national-poetry-month/', 
    view = poetry_month, 
    name = 'poetry_month', 
), 
    (r'^issues/',     include('myapp.apps.magazine.urls.issues')), 
    (r'^contributors/',    include('myapp.apps.magazine.urls.contributors')), 
    (r'^galleries/',    include('myapp.apps.magazine.urls.illustrations')), 
    (r'^multimedia/',    include('myapp.apps.multimedia.urls.multimedia')), 
    (r'^online/$',     direct_to_template, {'template': 'online.html'}), 

    #(r'^search/',     include('myapp.apps.search.urls')), 
    (r'^search/', include('solango.urls')), 
    (r'^textareas/', include('myapp.apps.textareas.urls')), 

    (r'',       include('myapp.apps.forms.urls')), 
    (r'^utils/',     include('myapp.apps.utils.urls')), 
    #(r'^rss/$',      'myapp.apps.magazine.views.rss_list'), 
    (r'^rss/huffpo/$',    redirect_to, {'url': '/articles/feeds/huffpo/', 'permanent': False}), 
    (r'^rss/googlenews/$',   redirect_to, {'url': '/articles/feeds/googlenews/', 'permanent': False}), 
    (r'^newsletter/',    include('myapp.apps.newsletter.urls')), 
    (r'^subscriptions/',   include('myapp.apps.newsubscriptions.urls')), 
    (r'^shared/',     include('myapp.apps.sharing.urls')), 
    (r'^counter/',     include('myapp.apps.counter.urls')), 

    # Redirects for legacy NYRB system 
    (r'^nyrev/(\w+)',       uber_legacy_article_redirect), 
    (r'^nyrev/',        redirect_to, {'url': '/'}), 
    (r'contents/(?P<legacy_date>\d+)/',  legacy_issue_detail_redirect), 
    (r'^archives/browse/?$',     legacy_browse_archives), 
    (r'^gallery/gallery-browse/?$',   legacy_gallery_browse), 
    (r'^gallery/',       legacy_illustration), 
    (r'authors/(?P<legacy_author_id>\d+)/', legacy_author_detail_redirect), 
    #(r'shop/product',       legacy_book_detail_redirect), 
    (r'^shop/product/?$',      legacy_product), 
    (r'^myapp/browse/?$',      legacy_book_browse), 

    (r'blogs/myapplog/post/(\d+)/(?P<slug>[-\w]+)/$', solango_blogsearch_redirect), 

    # URL shortening 
    (r'^u/(?P<short_url>[a-zA-Z0-9]+)/$', expand_url), 

    # NYRB shop 
    (r'^shop/',      include('myapp.apps.myapp_shop.urls')), 
    (r'^admin/shop/order/csv/?', csv_order_export_day), 
    (r'^admin/shop/order/(?P<order_id>\d+)/csv/?', csv_order_export), 

    # URLs for Savoy apps 
    (r'^tags/',      include('savoy.contrib.sections.tag_urls')), 
    (r'^podcasts/',     include('savoy.contrib.podcasts.urls')), 
    (r'^blogs/$',     redirect_to, {'url': "/blogs/myapplog/", 'permanent': False}), 
    (r'^blogs/',     include('savoy.contrib.blogs.urls')), 
    (r'^media/',     include('savoy.core.media.urls')), 

    # this is to use our own edit profile view 
    (r'^users/(?P<username>.+)/edit/$', edit_profile), 
    (r'^users/',     include('savoy.core.profiles.urls')), 

    # django-authopenid 
    (r'^account/getusername/',  get_lost_username), 

    #(r'account/signin/?',   'myapp.apps.forms.views.dual_login'), 
    (r'account/signin/?',   'django.contrib.auth.views.login', {'template_name': 'authopenid/signin.html'}), 
    (r'account/signout/?',   'django.contrib.auth.views.logout', {'next_page': '/'}), 
    (r'account/sendpw/?',   'myapp.apps.forms.views.dual_sendpw'), 
    (r'account/resetpw/?',   'myapp.apps.forms.views.reset_pw'), 
    (r'account/signup/?',   'myapp.apps.forms.views.link_and_signup'), 

    #(r'^account/',     include('django_authopenid.urls')), 

    # django-mailfriend 
    (r'^mail_friend/send/?',  email_link_send), 
    (r'^mail_friend/',    include('mailfriend.urls')), 

    # django.contrib.comments 

    # Django admin (Satchmo additions): 
    (r'^admin/print/(?P<doc>[-\w]+)/(?P<id>\d+)',      'shipping.views.displayDoc'), 
    (r'^admin/product/configurableproduct/(?P<id>\d+)/getoptions/', 'product.views.get_configurable_product_options'), 

    # Orders 
    (r'^admin/open-orders/$', 'myapp.apps.myapp_shop.views.open_orders'), 

    # Institutional subscription CSV 
    (r'^admin/subscriptions/institutionalsubscription\.csv', 'myapp.apps.subscriptions.views.institutional_sub_csv'), 

    # COUNTER admin for institutional reports 
    #(r'^admin/institution-counts/multiple-report/csv/$',   'myapp.apps.subscriptions.views.institution_multiple_csv'), 
    #(r'^admin/institution-counts/multiple-report/$',    'myapp.apps.subscriptions.views.institution_multiple_report'), 
    (r'^admin/institution-counts/institutions/(?P<id>\d+)/csv/', redirect_to, {'url': '/counter/%(id)s.csv'}), 
    (r'^admin/institution-counts/institutions/(?P<id>\d+)/$',  redirect_to, {'url': '/counter/%(id)s/'}), 
    #(r'^admin/institution-counts/process_file/?$',     'myapp.apps.subscriptions.views.institution_process_file'), 
    (r'^admin/institution-counts/$',        redirect_to, {'url': '/counter/'}), 

    # Django admin (standard) 
    (r'^admin/doc/',    include('django.contrib.admindocs.urls')), 
    (r'^admin/',     include(admin.site.urls)), 

    # custom feeds 
    (r'^feed/author/(?P<slug>[-\w]+)/$', AuthorFeed()), 
) 

# attach satchmo patterns after our patterns so we can override if needed 
from satchmo_store.urls import urlpatterns 
urlpatterns = myapp_patterns + urlpatterns 

if settings.DEBUG: 
    urlpatterns += patterns('django.views.static', 
     (r'media/(?P<path>.*)', 'serve', {'document_root': settings.MEDIA_ROOT}), 
    ) 
+0

向我們展示您的urlconf的消毒版本。 – agf 2012-04-12 21:33:51

+0

您對登臺和生產服務器有不同的local_settings.py文件嗎?兩者有什麼區別?我認爲反向也可以在視圖中調用。如果是這樣,對於視圖導入來說,一個分而治之的方法可能會發現問題。如果使用不同的虛擬環境,在每個環境中運行pip凍結可能會顯示一些庫差異。 – mtnpaul 2012-06-10 20:07:50

+0

您是否嘗試以某種方式調試此url配置?也許你可以在它裏面放一些打印文件(在不同的行中......例如:print myapp_patterns和print urlpatterns)然後,打開一個python shell並執行:from the_app_you_want import urls,你會看到怎麼回事與您的網址模式 – marianobianchi 2012-06-12 02:59:55

回答

2

我的猜測是,它與您的Satchmo模式和超載的名稱爲「URL模式」的Django的做專門尋找。

嘗試

from satchmo_store.urls import urlpatterns as satchmo_patterns 
urlpatterns = myapp_patterns + satchmo_patterns 

#etc. 
+0

好主意,但沒有解決問題。 – mipadi 2012-06-07 19:54:05

+2

上面的代碼片段在兩個地方有'from satchmo_store.urls import urlpatterns'。您是否嘗試註釋掉第一個,然後用'satchmo_store.urls將url模式導入爲satchmo_patterns'替換第二個?即使是satchmo文檔建議,「從satchmo_store.shop.urls導入urlpatterns作爲shoppatterns'。 – dgel 2012-06-07 20:17:42

+0

@dgel:給了一個嘗試,但仍然沒有去。 – mipadi 2012-08-29 22:09:03

0

奇怪的行爲可能是if settings.DEBUG之前bacause,URL模式是空的某些原因。

運行開發服務器時,它會從靜態應用程序中填充urlpatterns。在生產中運行時,它仍然是空的。

你可以用一個條款很容易地檢查這個喜歡

if urlpatterns: 
    # something is in urlpatterns 
    # What's its type, contents, ...? 
else: 
    # urlpatterns is empty 

一旦你想通了這一點,你可以深入挖掘。您還可以在單​​元測試中添加檢查,或者直接在python shell中進行檢查,方法是通過導入url模塊來使其更加健壯。

0

我認爲,這只是一種預感,你的URL也許很好,(因爲在你的staged開發機器中沒有問題,只能在產品上重新加載),問題可能在於gunicorn重新加載模塊的方式,方式python編譯的代碼在不同版本的解釋器之間運行,以及迭代器的行爲如何,但這又是所有的預感。

我建議你使用你在staged機器上生成的同一版本的python,看看你是否可以重新創建錯誤,如果已經有相同的版本,那麼它肯定是別的,我問的原因是大多數生產機器傾向於使用老版本的python,儘管我可能是錯的。

祝你好運。

0

取而代之的urlpatterns = myapp_patterns + urlpatterns東西,當然我們可以改爲調用myapp_patternsurlpatterns蝙蝠,並有它的結束裏面,沿着線的東西:

(r'^', include('satchmo_store.urls')), 

由於這是「Django的」方式包含網址格式?

這可能會解決您的問題,我不確定。值得一試,我猜。

+0

不幸的是,Satchmo有一些構建URL模式的複雜方式,所以通常的方法不起作用。 – mipadi 2012-08-29 22:08:14

相關問題