当 运行 服务器与 manage.py 时接收错误

Recieving errors when running server with manage.py

我正在尝试 运行 我的笔记本电脑上的服务器,当我在控制台中键入 '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", line 53, in wrapper
    fn(*args, **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() 

如果您需要查看任何其他文件,请提前致谢。

如果您错过了,请执行以下步骤,

第一步: 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 运行服务器

尝试用这个安装 django cron

pip install django-cron

我认为您启用了虚拟环境,然后尝试启动服务器 很多人都会犯这个错误。!!

你必须重新安装 django 需要的所有外部库(而不是 os math numpy 等基础库)

也通过此代码进行迁移

python manage.py makemigrations django-cron

这些都是愚蠢的错误