很多其他人遇到了错误No module named <appname>
。然而。我无法解决他们遇到的问题。我试图运行manage.py shell然后导入应用程序(博客)。有效。那我的代码有什么问题?
urls.py:
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^$', 'projectname.blog.views.index', name = 'index')
]
settings.py:
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'blog',
)
views.py:
from django.shortcuts import render
from blog.models import posts
def home(request):
return render('index.html', {'title': 'My First Post'})
错误讯息:
Request Method: GET
Request URL: http://localhost:8000/
Django Version: 1.8.5
Exception Type: ImportError
Exception Value:
No module named blog
Exception Location: /usr/lib/python2.7/importlib/__init__.py in import_module, line 37
Python Executable: /usr/bin/python
Python Version: 2.7.6
答案 0 :(得分:4)
有几个问题:
projectname
home
,而不是index
因此,您应该使用:
url(r'^$', 'blog.views.home', name='index')
答案 1 :(得分:0)
请勿在特定应用的路径中使用您的项目名称。而不是
url(r'^$', 'projectname.blog.views.index', name = 'index')
使用
url(r'^$', 'blog.views.index', name = 'index')
答案 2 :(得分:0)
在views.py中,您定义了主页而不是索引 尝试以下
def index(request):
return render('index.html', {'title': 'My First Post'})