本文介绍了ModuleNotFoundError:Django中没有名为"posts"的模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我运行 python manage.py makemigrations
时,未发现模块错误,请帮助
when i run python manage.py makemigrations
,no module error found occur, plz help me
settings.py
settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'posts'
]
urls.py
from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
from django.urls import path, include
from posts.views import index, blog, post
urlpatterns = [
path('admin/', admin.site.urls),
path('', index),
path('blog/', blog),
path('post/', post),
]
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL,
document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT)
post>models.py
from django.db import models
from django.contrib.auth import get_user_model
# Create your models here.
User =get_user_model()
class Author(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
profile_picture = models.ImageField()
def __str__(self):
return self.user.username
class Category(models.Model):
title = models.CharField(max_length=20)
def __str__(self):
return self.title
class Post(models.Model):
title = models.CharField(max_length=100)
overview = models.TextField()
timestamp = models.DateTimeField(auto_now_add=True)
comment_count = models.IntegerField(default = 0)
author = models.ForeignKey(Author, on_delete=models.CASCADE)
thumbnail = models.ImageField()
categories = models.ManyToManyField(Category)
def __str__(self):
return self.title
(env) C:\Users\Dell\project4\src>python manage.py makemigrations
Traceback (most recent call last):
File "manage.py", line 21, in <module>
main()
File "manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "C:\Users\Dell\Envs\env\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
utility.execute()
File "C:\Users\Dell\Envs\env\lib\site-packages\django\core\management\__init__.py", line 357, in execute
django.setup()
File "C:\Users\Dell\Envs\env\lib\site-packages\django\__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\Users\Dell\Envs\env\lib\site-packages\django\apps\registry.py", line 91, in populate
app_config = AppConfig.create(entry)
File "C:\Users\Dell\Envs\env\lib\site-packages\django\apps\config.py", line 90, in create
module = import_module(entry)
File "C:\Users\Dell\Envs\env\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 965, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'posts'
推荐答案
在 INSTALLED_APPS
内部,您应该这样声明您的 posts
:'posts.apps.PostsConfig'
要了解原因,请查看项目文件夹中的文件 apps.py
;它应该看起来像这样:
Inside INSTALLED_APPS
you should declare your posts
like that: 'posts.apps.PostsConfig'
To understand why, take a look at file apps.py
from your project folder; it should look like that:
from django.apps import AppConfig
class PostsConfig(AppConfig):
name = 'posts'
这篇关于ModuleNotFoundError:Django中没有名为"posts"的模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!