【问题标题】:Django python - Module not foundDjango python - 找不到模块
【发布时间】:2014-07-19 19:50:53
【问题描述】:

我是新手,所以我可能做了一些愚蠢的事情。运行 python 3.3 和 Django 1.6.2。

当我通过命令行运行本地服务器时,这是我收到“P/1.1 404 1712”的错误,浏览器上的错误是“找不到模块”,异常位置指向 urls.py 第 22 行;

document_root=settings.STATIC_ROOT)

这是 urls.py 的一部分:

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

from django.conf import settings
from django.conf.urls import static



from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    # Examples:
    url(r'^$', 'signups.views.home', name='home'),
    # url(r'^blog/', include('blog.urls')),
    url(r'^admin/', include(admin.site.urls)),

)


if settings.DEBUG:
    urlpatterns += static(settings.STATIC_URL,
                          document_root=settings.STATIC_ROOT)

    urlpatterns += static(settings.MEDIA_URL,
                          document_root=settings.MEDIA_ROOT)

这就是我的 settings.py 的样子:

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.6/howto/static-files/

STATIC_URL = '/whattheheck/static/'

# Template location
TEMPLATE_DIRS = {
    os.path.join(os.path.dirname(BASE_DIR), "whattheheck", "static", "templates"),

}

if DEBUG:
    MEDIA_URL = '/whattheheck/media/'
    STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "whattheheck", "static", "static-only")
    MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), "whattheheck", "static", "media")
    STATICFLIES_DIRS = (
        os.path.join(os.path.dirname(BASE_DIR), "whattheheck", "static", "static")

    )

有人可以帮忙吗?

【问题讨论】:

  • 变量 DEBUG 在哪里?看起来它没有被设置,所以你试图调用的所有东西都没有被正确执行。
  • @TimCastelijns 第 22 行是 document_root=settings.STATIC_ROOT),它是 urls.py 代码块的一部分:if settings.DEBUG: urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
  • @NicholasYoung 调试在settings.py中; DEBUG = True TEMPLATE_DEBUG = DEBUG ALLOWED_HOSTS = []
  • 请提供“Module not found”错误的完整回溯。
  • 等等,你为什么要在 url 中包含文件路径?你一开始就不应该。我也有点累,但我不认为当你试图将变量包含到脚本中时 if 会运行。我可能错了

标签: python django django-settings


【解决方案1】:

你在导入语句中忘记了一个staticsee the documentation

from django.conf.urls.static import static
                    # ^^^^^^ this one

现在,它尝试将static 模块用作函数,但显然它不起作用。当您尝试使用模块对象(例如ossys 或任何第三方)作为可调用对象(使用__call__ 方法)时,会引发错误'module' object is not callable

【讨论】:

  • 哦,呃,我完全忽略了那个 xD
  • 它仍然没有解释为什么我得到了反对票(即使不是你):(
  • 我不知道,不是我。 Stackoverflow 让我有点害怕人们多么希望一切都完美。
  • @MaximeLorant 嘿 Maxine,谢谢...不知道谁给了你一个downvoter,对不起:(。它不允许我给你投票,因为我没有最低要求的声誉15 人...但我肯定会在获得声誉后立即投反对票。非常感谢 x。我现在面临的另一个问题是我的 css 文件没有加载。如果你愿意,我会创建另一个问题,因为您对此投了反对票...请告诉我?
  • @MaximeLorant 给你,我现在有足够的声望给你投票。感谢您的帮助:)
猜你喜欢
  • 2011-03-15
  • 2017-09-01
  • 1970-01-01
  • 2018-08-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-03
相关资源
最近更新 更多