2009-06-15 72 views
3

所以我剛剛開始玩Django,我決定嘗試在我的服務器上嘗試。所以我安裝Django和創建一個新的項目,按照教程中介紹的Djangoproject.comDjango ImportError在/無論我做什麼

基礎

不幸的是,無論我做什麼,我不能讓意見的工作:我不斷獲得

ImportError at/

No module named index 

Here是這個錯誤

我一直在谷歌上搜索,並沒有運氣嘗試各種命令的截圖,我真的即將撕我的頭髮,直到我變成禿頭。我已經嘗試將django源目錄,我的項目目錄和應用程序目錄添加到PYTHONPATH中,但沒有運氣。我也確保init .py在所有的目錄(包括項目和應用程序)中有沒有人有任何想法可以在這裏出錯?

最新通報

對不起,我是實物倉促而張貼這,這裏的一些背景:

我一直在試圖將服務器只是Django的使用manage.py(建於服務器蟒manage.py 0.0.0.0:8000,因爲我需要從外部訪問的話)在Linux(Debian的)

APPDIR/views.py

from django.http import HttpResponse 

def index(request): 
    return HttpResponse("Sup") 

def test(request): 
    return HttpRespons("heyo") 

urls.py

from django.conf.urls.defaults import * 

# Uncomment the next two lines to enable the admin: 
from django.contrib import admin 
admin.autodiscover() 

urlpatterns = patterns('', 
    # Example: 
    # (r'^****/', include('****.foo.urls')), 

    # Uncomment the admin/doc line below and add 'django.contrib.admindocs' 
    # to INSTALLED_APPS to enable admin documentation: 
    # (r'^admin/doc/', include('django.contrib.admindocs.urls')), 

    # Uncomment the next line to enable the admin: 
    (r'^admin/', include(admin.site.urls)), 
    (r'^test/', include('mecore.views.test')), 
    (r'^', include('mecore.views.index')) 
) 
+0

您還有什麼可以給我們的背景嗎?例如哪個模塊引發ImportError。堆棧跟蹤會很有幫助。 – 2009-06-15 22:40:39

+0

剛剛更新,希望這會幫助你們。 – 2009-06-15 23:52:29

+0

@Sliggy:請不要發佈錯誤的截圖。複製並粘貼實際網頁中的實際文字,比屏幕截圖更有用。 – 2009-06-16 00:41:32

回答

12

urls.py是錯誤的;你應該考慮閱讀thisthis

您不包含函數;你包含一個模塊。你命名一個函數,mecore.views.index。您只包含整個模塊include('mecore.views')

from django.conf.urls.defaults import * 

# Uncomment the next two lines to enable the admin: 
from django.contrib import admin 
admin.autodiscover() 

urlpatterns = patterns('', 
    # Example: 
    # (r'^****/', include('****.foo.urls')), 

    # Uncomment the admin/doc line below and add 'django.contrib.admindocs' 
    # to INSTALLED_APPS to enable admin documentation: 
    # (r'^admin/doc/', include('django.contrib.admindocs.urls')), 

    # Uncomment the next line to enable the admin: 
    (r'^admin/', include(admin.site.urls)), 
    (r'^test/', 'mecore.views.test'), 
    (r'^', 'mecore.views.index') 
) 
3

你有沒有在每個mecore和看法目錄__init__.py,以及在意見index.py?

從Python的角度來看,目錄是一個包,只有它有一個名爲__init__.py的文件(它可以是空的,如果在導入包時不需要執行任何特殊代碼,但它必須是那裏)。

編輯:請注意,在include必須命名Python路徑的模塊,而不是一個函數:看Django's relevant docs - 從您的評論來看,你似乎是誤用include,因爲我看到@美國洛特不得不在他的回答中推測。

-1

ImportError No module named views

嘗試和移動views.py的 「內部」 mysite的目錄。視圖是應用程序的一部分,因此需要將它們移到應用程序目錄中(而不是在項目目錄中)。

您收到的錯誤消息表示mysite(應用程序)沒有views.py模塊。

相關問題