【问题标题】:Django ImportError at / no matter what I doDjango ImportError at / 不管我做什么
【发布时间】:2010-11-03 04:16:28
【问题描述】:

所以我刚刚开始使用 Django,我决定在我的服务器上试一试。所以我安装了 Django 并创建了一个新项目,遵循 Djangoproject.com 上的教程中概述的基础知识

不幸的是,无论我做什么,我都无法让视图起作用:我不断地得到 ​​p>

ImportError at /

No module named index

Here是这个错误的截图

我一直在谷歌上搜索并尝试各种命令,但都没有成功,而且我真的要把头发扯掉,直到我秃顶。我尝试将 django 源目录、我的项目目录和应用程序目录添加到 PYTHONPATH,但没有成功。我还确保 init.py 位于所有目录(项目和应用程序)中有人知道这里可能出了什么问题吗?

更新

抱歉,我发这个的时候有点着急,这里有一些上下文:

我一直在尝试的服务器只是 django 在 linux (debian) 上使用 manage.py (python manage.py 0.0.0.0:8000,因为我需要从外部访问它) 的内置服务器

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'))
)

【问题讨论】:

  • 您能提供更多背景信息吗?例如哪个模块引发了 ImportError。堆栈跟踪会很有帮助。
  • 刚刚更新,希望对大家有所帮助。
  • @Sliggy:请不要发布错误截图。从实际网页复制并粘贴实际文本,它比屏幕截图更有用。
  • 您可能没有注意到,但您在 test() 方法中也拼错了“HttpResponse”。最后错过了“e”。

标签: python django


【解决方案1】:

您的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')
)

【讨论】:

  • 我只是添加了一些源文件和一些信息,我认为这应该有助于我们找到问题。
  • 我爱你。就像,说真的,我非常爱你。随意生我的孩子。
【解决方案2】:

在每个 mecore 和 views 目录中是否有 __init__.py,以及在视图中是否有 index.py?

从 Python 的角度来看,一个目录是一个包,只有当它有一个名为 __init__.py 的文件时(如果在导入该包时不需要执行任何特殊代码,它可能是空的,但它必须在那里)。

编辑:请注意,在include 中,您必须将python 路径命名为模块,而不是函数:请参阅Django's relevant docs - 从您的评论来看,您似乎误用了include,正如我所见@S.Lott 在他的回答中进行了推测。

【讨论】:

  • 没有mecore/views/目录。只有 /mecore/ 和 views.py 是 /mecore/ 中的一个文件,其中包含所有功能,如 Django 教程(我相信第 3 部分)中所述
  • 嗯,肯定不是 include() 吗? include() 必须包含模块的 Python 路径,而不是函数。
【解决方案3】:

来自ImportError No module named views

尝试将views.py 移动到“内部”mysite 目录。视图是应用程序的一部分,因此需要将它们移动到应用程序目录中(而不是项目目录中)。

您收到的错误消息表明mysite(应用程序)没有views.py 模块。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-16
    • 1970-01-01
    • 1970-01-01
    • 2010-10-10
    • 2018-08-14
    相关资源
    最近更新 更多