问题描述
我试图在笔记本电脑上运行服务器,当在控制台中键入“ python manage.py runserver”时 我收到一些错误。可能是我需要导入一些我尝试过'pip install python-cron'的模块,但这没有用。 错误提示:
[2020-11-10 09:04:47,241] autoreload: INFO - Watching for file changes with StatReloader
Exception in thread django-main-thread:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/threading.py",line 932,in _bootstrap_inner
self.run()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/threading.py",line 870,in run
self._target(*self._args,**self._kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/utils/autoreload.py",line 53,in wrapper
fn(*args,**kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/core/management/commands/runserver.py",line 109,in inner_run
autoreload.raise_last_exception()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/utils/autoreload.py",line 76,in raise_last_exception
raise _exception[1]
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/core/management/__init__.py",line 357,in execute
autoreload.check_errors(django.setup)()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/utils/autoreload.py",**kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/__init__.py",line 24,in setup
apps.populate(settings.INSTALLED_APPS)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/apps/registry.py",line 91,in populate
app_config = AppConfig.create(entry)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/apps/config.py",line 90,in create
module = import_module(entry)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/importlib/__init__.py",line 127,in import_module
return _bootstrap._gcd_import(name[level:],package,level)
File "<frozen importlib._bootstrap>",line 1014,in _gcd_import
File "<frozen importlib._bootstrap>",line 991,in _find_and_load
File "<frozen importlib._bootstrap>",line 973,in _find_and_load_unlocked
ModuleNotFoundError: No module named 'django_cron'
我拥有的cron.py文件是:
from django.contrib.auth.models import User
import os
import datetime
from crontab import CronTab
#from django_common.helper import send_mail
from django_cron import CronJobBase,Schedule
from .models import Photo
from PIL import Image
class PhotoDeleteCronJob(CronJobBase):
RUN_EVERY_MINS = 1
schedule = Schedule(run_every_mins=RUN_EVERY_MINS)
code = 'cron.PhotoDeleteCronJob'
def do(self):
delet = Photo.objects.all()
delet.delete()
如果需要查看其他文件,请先感谢。
解决方法
如果错过了,请执行以下步骤,
步骤1:
pip install django-cron
步骤2:
打开settings.py并将其注册到您的installed_app
djnago-cron
步骤3:
您需要通过在settings.py中安装应用程序后进行迁移
python manage.py makemigrations djnago-cron
第4步:
比迁移这些更改
python manage.py migrate
第5步: 现在您可以开始运行,并通过 `python manage.py runserver
,尝试与此安装django cron
pip install django-cron
我认为您已经启用了虚拟环境,然后尝试启动服务器 许多人犯了这个错误!!
您必须重新安装Django所需的所有外部库(而不是os math numpy等基本库)
也通过此代码进行迁移
python manage.py makemigrations django-cron
这些都是愚蠢的错误