从芹菜功能发出推送通知

2024-03-28 13:06:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图使用Django消息显示消息,但当@shared_任务上发生某个事件时,应执行此警报。 这是tasks.py中的芹菜函数代码

def show_message_in_time(self,response):
    while True:
        # get_info_to_show() this function will do something 
        messages.info(response, 'Your email is added successfully!')
        sleep(2*60)

这是views.py上的函数

def index(response):
    
    show_message_in_time.delay(response)
    return render(response, "Sales.html", {'zipped_list': objs})

下面是我收到的错误

Exception Value:    
Object of type 'WSGIRequest' is not JSON serializable

我曾尝试将WSGIRequest转换为JSON,但也没有成功。 任何帮助都将不胜感激


Tags: django函数inpyinfojson消息message
1条回答
网友
1楼 · 发布于 2024-03-28 13:06:09

解决问题的最简单的方法是用您真正需要的WSGIRequest/HttpRequest片段创建一个字典

假设您的WSGIRequest包含“用户名”和“电子邮件”部分。然后将索引重构为如下内容:

def index(response):
    data = { 
        "method": response.method,
        "username": response["username"],
        "email": response["email"]
    }

    if response.method == "POST":
        data["response_data"] = response.POST

    if response.method == "GET":
        data["response_data"] = response.GET

    show_message_in_time.delay(data)
    return render(response, "Sales.html", {'zipped_list': objs})

如果您知道这是一种post方法,那么最快的测试是

def index(response):
    # POST is a dictionary, so it is definitely serializable
    show_message_in_time.delay(response.POST)
    return render(response, "Sales.html", {'zipped_list': objs})

免责声明:我从未使用过Django或任何其他web框架,但我对芹菜知之甚少

相关问题 更多 >