2009-06-15 64 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模块。

相关问题