【发布时间】:2020-05-04 13:57:21
【问题描述】:
我使用 django 处理通过 typeform 的 api(类似于 google 表单的服务)接收的 json 中的用户数据。提交他们的 typeforms 后,用户被重定向到我的 django 服务器,该服务器将处理后的数据传递到显示用户分数的登录页面的链接。
这工作了一段时间,但在我的算法的不同版本反复多次之后,views.py 函数被调用了两次。更奇怪的是 - 变量被覆盖,考虑到我的代码结构,这不应该是可能的。下面列表中的每个分数不应高于 40,但它会成倍增加。在我重置服务器后,它再次很好,但仅适用于一个 GET 请求,然后无论它是同一个用户的电子邮件(用于识别他/她的提交数据)还是不同的电子邮件,它都会再次变得混乱。这是它在我的控制台中的外观:
Django version 3.0.3, using settings 'demo_server.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
# First request:
user's scores: [['A', 30], ['R', 24], ['E', 20], ['I', 16], ['S', 16], ['C', 14]]
{correct link}
[04/May/2020 15:01:23] "GET /typeform/dan@hiry.pl HTTP/1.1" 302 0
# Second request:
user's scores: [['A', 100], ['R', 82], ['E', 70], ['I', 58], ['S', 58], ['C', 52]]
(i've added a few lines to identify when the scores are to high to redirect users to an error page)
{error page link}
[04/May/2020 15:01:26] "GET /typeform/dan@hiry.pl HTTP/1.1" 302 0
[04/May/2020 15:01:26] "GET /typeform/home HTTP/1.1" 200 42
# as you can see above, there are two requests happening the same second instead of one
我发现关于这个问题的唯一线程要么很老,而且专注于我迄今为止没有使用过的 django 表单(我试图让我的脚本尽可能简单)和/或完全没有解决。我试图至少抑制脚本中的重定向链接(例如quit()),但没有任何效果。
提前致谢,祝大家有个美好的一天:)
编辑 1:根据要求,这是我的 views.py 的外观:
from django.http import HttpResponse
from django.shortcuts import redirect
import time
import json
import requests
def typeform_redirect(request, tmail='kosma@hiry.pl'):
# here the correct link for the user with a matching email is generated
print(link)
response = redirect(link)
return response
【问题讨论】:
-
您能否提供有关您的视图和网址的代码?
-
@arianhf 恐怕我不能全部分享,因为它是公司的知识产权 + 我的 views.py 上的代码大约有 800 行(我想拥有这样的代码并不是最好的解决方案views.py 中的长函数,但我需要一种特别的方法来使其工作,并且直到最近它都做得很好),我认为几乎没有甚至没有可能与这个问题有关。我已经编辑了这篇文章,至少包含了它的基本结构。
-
只是为了确定,在 typeform 设置中,您将用户重定向到
/typeform/{email_answered_in_the_typeform}?双重加载是否仅在从 typeform 重定向后发生?或者当您手动打开 URL 时? -
@NicolasGrenié 1) 确实如此。 2) 它发生在从 typeform 重定向以及手动输入 url 时。
-
创建了一个新的 django 项目,但问题仍然存在,所以我 95% 肯定它与views.py有关
标签: python django redirect server