【问题标题】:Error from Tasks job when referencing a model that uses from django.contrib.auth.models import User引用使用 from django.contrib.auth.models import User 的模型时,任务作业出错
【发布时间】:2020-10-15 21:09:33
【问题描述】:

我的 blog.models 正在使用:

from django.contrib.auth.models import User

该模型是从我每小时作为任务运行的脚本中调用的。这是我看到的错误:

Traceback (most recent call last):
  File "/home/redinv/src/cron.py", line 8, in <module>
    from blog.models import ImagePost, MyPost, ScheduledPost
  File "/home/redinv/src/blog/models.py", line 3, in <module>
    from django.contrib.auth.models import User
  File "/usr/lib/python3.8/site-packages/django/contrib/auth/models.py", line 2, in <module>
    from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
  File "/usr/lib/python3.8/site-packages/django/contrib/auth/base_user.py", line 47, in <module>
    class AbstractBaseUser(models.Model):
  File "/usr/lib/python3.8/site-packages/django/db/models/base.py", line 103, in __new__
    app_config = apps.get_containing_app_config(module)
  File "/usr/lib/python3.8/site-packages/django/apps/registry.py", line 252, in get_containing_app_config
    self.check_apps_ready()
  File "/usr/lib/python3.8/site-packages/django/apps/registry.py", line 134, in check_apps_ready
    settings.INSTALLED_APPS
  File "/usr/lib/python3.8/site-packages/django/conf/__init__.py", line 79, in __getattr__
    self._setup(name)
  File "/usr/lib/python3.8/site-packages/django/conf/__init__.py", line 60, in _setup
    raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

2020-06-25 00:50:25 -- Completed task, took 3.95 seconds, return code was 1.

在我的 INSTALLED_APPS = 下的设置文件中,我已经有了:

'django.contrib.auth',

这段代码当然是在我的机器上本地运行的,我想弄清楚这里有什么问题?

我尝试从两个位置运行该脚本以查看在尝试查找导入文件时是否存在问题:

/home/username/src
/home/username/src/blog

两者的结果相同。

感谢任何帮助,我是 python 的新手。

谢谢

【问题讨论】:

    标签: python django scheduled-tasks pythonanywhere


    【解决方案1】:

    如果你想从网站上下文之外运行 Django 代码,你需要先初始化 Django。有两种方法可以做到这一点:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-19
      • 1970-01-01
      • 1970-01-01
      • 2022-12-24
      • 2012-06-05
      • 1970-01-01
      • 2021-12-23
      • 1970-01-01
      相关资源
      最近更新 更多