简体   繁体   English

ImportError:未命名模块<appname>

[英]ImportError: No module named <appname>

A lot of other people have come across the error No module named <appname> . 许多其他人遇到了错误No module named <appname>的错误。 However. 然而。 I couldn't relate to the problem they had. 我无法解决他们遇到的问题。 I tried to run manage.py shell and then imported the app (blog). 我尝试运行manage.py shell,然后导入了该应用程序(博客)。 It worked. 有效。 So what is wrong in my code? 那么我的代码有什么问题呢?

urls.py: 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: 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: views.py:

from django.shortcuts import render

from blog.models import posts

def home(request):
    return render('index.html', {'title': 'My First Post'})

Error message: 错误信息:

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

A couple of issues: 几个问题:

  1. Your project is not a package, so you shouldn't be adding projectname 你的项目是不是一个包,所以你不应该增加projectname
  2. Your view name is home , not index 您的视图名称是home而不是index

Therefore, you should be using: 因此,您应该使用:

url(r'^$', 'blog.views.home', name='index')

Don't use your project name in path to specific app. 不要在特定应用程序的路径中使用项目名称。 Instead of 代替

url(r'^$', 'projectname.blog.views.index', name = 'index')

use 采用

url(r'^$', 'blog.views.index', name = 'index')

In your views.py you were defined the home instead of index Try below one 在您的views.py中,您已定义房屋而不是索引尝试以下一项

def index(request):
    return render('index.html', {'title': 'My First Post'})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM