我在撰写一本书时遇到困难,该书录用了目录,把档案装入我的数据库,用于Django项目。 根据要求,笔记本必须放在一份单独的目录上(M django目录)。
Here is my project structure currently:
Main-Project/
database/
text-script.py
text-files/
example-file.txt
django/
django/
settings.py
snippets/
models.py
我的案文。
import os
import sys
import django
current_dir = os.path.dirname(os.path.abspath(__file__))
parent_dir = os.path.dirname(current_dir)
sys.path.append(parent_dir)
os.environ.setdefault( DJANGO_SETTINGS_MODULE , django.django.settings )
django.setup()
from django.snippets.models import Snippet
def import_articles(directory):
for filename in os.listdir(directory):
if filename.endwith( .txt ):
with open(os.path.join(directory, filename), r ) as file:
content = file.read()
Snippet.objects.create(filename=filename, content=content)
if __name__ == __main__ :
text_dir = os.path.join(current_dir, text-files )
import_articles(text_dir)
我已安装了这样的镜头:
INSTALLED_APPS = [
django.contrib.admin ,
django.contrib.auth ,
django.contrib.contenttypes ,
django.contrib.sessions ,
django.contrib.messages ,
django.contrib.staticfiles ,
snippets.apps.SnippetsConfig ,
]
当我试图操作文字时,我会发现这一错误:
ModuleNotFoundError: No module named snippets
我如何正确把Snippet模型从我的Django项目上排出,以便利用我在Django项目中使用的同一数据库?