python - Django的TypeError : the first argument must be callable when I import a scheduler in my views. py文件?

标签 python django python-3.x django-views

我正在使用 Django 1.11。我想在我的应用程序中使用调度程序每天运行一次我的脚本。

这是我的view.py文件

from __future__ import print_function
from django.shortcuts import render
from django.utils import timezone
from django.http import HttpResponse
from datetime import datetime, timedelta
import requests
import schedule
import time


def republic(request):
    return HttpResponse("<h1>Success Hindustan</h1>")


def indiatv(request):
    return HttpResponse("<h1>Success Hindustan</h1>")

def ndtv(request):
    return HttpResponse("<h1>Success NDTV</h1>")


schedule.every().day.at("17:19").do(republic(requests))
schedule.every().day.at("17:19").do(indiatv(requests))
schedule.every().day.at("17:19").do(ndtv(requests))

while 1:
    schedule.run_pending()
    time.sleep(1)

当我运行服务器时出现以下错误

 File "/home/imsaiful/PiroProject/pironews/feed/urls.py", line 2, in <module>
    from . import views
  File "/home/imsaiful/PiroProject/pironews/feed/views.py", line 230, in <module>
    schedule.every().day.at("17:19").do(republic(requests))
  File "/home/imsaiful/anaconda3/lib/python3.6/site-packages/schedule/__init__.py", line 385, in do
    self.job_func = functools.partial(job_func, *args, **kwargs)
TypeError: the first argument must be callable

但是当我删除调度程序行时,应用程序运行正常。

最佳答案

republic(requests) will return HttpResponse,

所以执行会是

schedule.every().day.at("17:19").do(HttpResponse)

do 方法中你应该提到函数,而不是类实例。 您可以使用以下之一

解决方案一:

schedule.every().day.at("17:19").do(lambda: republic(requests))

解决方案 2.

schedule.every().day.at("17:19").do(republic, requests)

解决方案 3.

import functools
schedule.every().day.at("17:19").do(functools.partial(republic, requests))

关于python - Django的TypeError : the first argument must be callable when I import a scheduler in my views. py文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52166054/

相关文章:

python - 如何在matplotlib中绘制平均线?

python - Pandas 添加两个多索引数据帧

python - 如何获取列表列表中可能对的列表 - python

python - 如何在 python/django 中使用 URL 保存图像

python - Django - 数据库数据未显示在应用程序中?

python - 过滤 Pandas 行,其中列中的第一个字母是/不是某个值

python - 使用Python以MySQL json列格式插入Json数据

python - 查找文本中每个句子的长度

python - 加载pickle时出错

python - Django 静态文件相对引用 Amazon S3