ImportError:没有名为blog Django的模块

时间:2021-01-29 20:26:27

I am new in python i try to create blog in django, when i try to create module by typing python manage.py startapp blog this command

我是python中的新手我尝试在django中创建博客,当我尝试通过输入python manage.py startapp blog来创建模块时这个命令

python version : 2.7 django-admin version : 1.8

python版本:2.7 django-admin版本:1.8

This is urls.py file

这是urls.py文件

url(r'^$', 'logicmindblog.blog.views.index'),
url(r'^blog/view/(?P<slug>[^\.]+).html', 'logicmindblog.blog.views.view_post', name='view_blog_post'),
url(r'^blog/category/(?P<slug>[^\.]+).html', 'logicmindblog.blog.views.view_category', name='view_blog_category'),

In settings.py INSTALLED_APPS when i add 'logicmindblog.blog', and try to runserver It give me error ImportError: No module named blog Django

在settings.py INSTALLED_APPS中,当我添加'logicmindblog.blog',并尝试runserver它给我错误ImportError:没有模块名为博客Django

But when i remove project name and just add 'blog' in and run server this migration and admin working fine, i can add blog , blog category from admin section

但是,当我删除项目名称,只是添加'博客'并运行服务器这个迁移和管理工作正常,我可以添加博客,博客类别从管理部分

Can anyone help me figure this, Thanks in advance

任何人都可以帮我解决这个问题,在此先感谢

logicmindblog/
├── blog
│   ├── admin.py
│   ├── admin.pyc
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── migrations
│   │   ├── 0001_initial.py
│   │   ├── 0001_initial.pyc
│   │   ├── __init__.py
│   │   └── __init__.pyc
│   ├── models.py
│   ├── models.pyc
│   ├── __pycache__
│   │   └── __init__.cpython-35.pyc
│   ├── tests.py
│   ├── views.py
│   └── views.pyc
├── db.sqlite3
├── logicmindblog
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── __pycache__
│   │   ├── __init__.cpython-35.pyc
│   │   └── settings.cpython-35.pyc
│   ├── settings.py
│   ├── settings.pyc
│   ├── urls.py
│   ├── urls.pyc
│   ├── views.py
│   ├── views.pyc
│   ├── wsgi.py
│   └── wsgi.pyc
├── manage.py
└── views
    ├── base.html
    ├── index.html
    ├── view_category.html
    └── view_post.html

2 个解决方案

#1


2  

Only the app name blog should be added in settings.py

只应在settings.py中添加应用名称博客

settings.py

INSTALLED_APPS = [
    #Custom Apps
    'blog',
    . . .
    . . .

    #django apps
    'django.contrib.admin',
    'django.contrib.auth',
    . . . 
    . . . 
]

In urls.py

from .views import index, view_post, view_category

urlpatterns = [
    url(r'^$', index, name='view-blog-index'),
    url(r'^blog/view/(?P<slug>[^\.]+).html', view_post, name='view-blog-post'),
    url(r'^blog/category/(?P<slug>[^\.]+).html', view_category, name='view-blog-category'),
]

#2


1  

You should use this syntax when defining your urls:

在定义网址时应使用此语法:

from blog.views import index, view_post, view_category

urlpatterns = [
  url(r'^$', index, name='view-blog-index'),
  url(r'^blog/view/(?P<slug>[^\.]+).html', view_post, name='view-blog-post'),
  url(r'^blog/category/(?P<slug>[^\.]+).html', view_category, name='view-blog-category'),
]

Take a look at the Django documentation on this topic: link

看一下关于这个主题的Django文档:link

Also, note that the official documentation recommends using hypens instead of underscores when naming your url patterns: link

另请注意,官方文档建议在命名您的网址模式时使用超级而非下划线:链接

Edit: as Astik pointed, you don't need to put logicmindblog.blog in the INSTALLED_APPS, you can just put blog.

编辑:正如Astik指出的那样,你不需要将logicmindblog.blog放在INSTALLED_APPS中,你可以放置博客。

#1


2  

Only the app name blog should be added in settings.py

只应在settings.py中添加应用名称博客

settings.py

INSTALLED_APPS = [
    #Custom Apps
    'blog',
    . . .
    . . .

    #django apps
    'django.contrib.admin',
    'django.contrib.auth',
    . . . 
    . . . 
]

In urls.py

from .views import index, view_post, view_category

urlpatterns = [
    url(r'^$', index, name='view-blog-index'),
    url(r'^blog/view/(?P<slug>[^\.]+).html', view_post, name='view-blog-post'),
    url(r'^blog/category/(?P<slug>[^\.]+).html', view_category, name='view-blog-category'),
]

#2


1  

You should use this syntax when defining your urls:

在定义网址时应使用此语法:

from blog.views import index, view_post, view_category

urlpatterns = [
  url(r'^$', index, name='view-blog-index'),
  url(r'^blog/view/(?P<slug>[^\.]+).html', view_post, name='view-blog-post'),
  url(r'^blog/category/(?P<slug>[^\.]+).html', view_category, name='view-blog-category'),
]

Take a look at the Django documentation on this topic: link

看一下关于这个主题的Django文档:link

Also, note that the official documentation recommends using hypens instead of underscores when naming your url patterns: link

另请注意,官方文档建议在命名您的网址模式时使用超级而非下划线:链接

Edit: as Astik pointed, you don't need to put logicmindblog.blog in the INSTALLED_APPS, you can just put blog.

编辑:正如Astik指出的那样,你不需要将logicmindblog.blog放在INSTALLED_APPS中,你可以放置博客。