ImportError:没有名为' index'的模块

时间:2016-04-17 04:26:24

标签: python django

我无法找出错误的来源,这些是我错过的最后几个文件。我得到了ImportError:没有名为' index'。

的模块
from django.conf.urls import url

from . import views

from .models import Album


urlpatterns = [
    #/index.html/
    url(r'^$', views.index ,name='index'),
    #/index.html/71/
    url(r'^(?P<album_id>[0-9]+)/$', views.details, name='detail')
]

________________________________意见__________________________________

from django.shortcuts import render
from django.http import HttpResponse
from .models import Album
from django.template import loader

# Create your views here.

def index(request):
    all_albums = Album.objects.all()
    template = loader.get_template('/music/index.html')
    context = {
        'all_albums': all_albums,
    }
    return HttpResponse(template.render(context,request))

def details(request, album_id):
    return HttpResponse("<h2> details for album id: " + str(album_id) + "</h2>")

-----------------------------追溯---------------- -----------------

Tyrees-MacBook-Pro:tyree_website tyreestevenson$ python3 manage.py runserver
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x103b7eb70>
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/utils/autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/core/management/commands/runserver.py", line 109, in inner_run
    autoreload.raise_last_exception()
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/utils/autoreload.py", line 249, in raise_last_exception
    six.reraise(*_exception)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/utils/six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/utils/autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/__init__.py", line 18, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/apps/registry.py", line 85, in populate
    app_config = AppConfig.create(entry)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/apps/config.py", line 116, in create
    mod = import_module(mod_path)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 944, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 944, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 956, in _find_and_load_unlocked
ImportError: No module named 'index'

1 个答案:

答案 0 :(得分:0)

我不确定你为什么会收到这个错误。

如果您发表评论url(r'^$', views.index ,name='index'),会怎样?

您是否尝试使用patterns快捷方式(并且没有相对导入)?例如:

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

urlpatterns += patterns('your_app.views',
    #/index.html/
    url(r'^$', 'index' ,name='index'),
    #/index.html/71/
    url(r'^(?P<album_id>[0-9]+)/$', 'details', name='detail')
)