如何在 django 中等待端点请求到达?
告诉我您可以用什么来等待另一个端点的响应? 我在主页(索引)上,在表单中输入一些内容。 POST 请求被发送到另一台服务器。此时:
- 另一台服务器处理数据,并根据其正确性向我的 url /answer 发出 POST 请求(True 或 False)。
- 例如,我将被重定向到另一个页面。 如何注册另一个页面(另一个)的逻辑,以便 Django 等待从另一个服务器到 /answer 的 POST 请求,并根据此请求 True/False,我在此页面上输出所有 OK 或所有 Bad ?
url.py
urlpatterns = [
path('index/', index, name='index'),
path('page_2/', page_2, name='page_2'),
path('answer/', answer, name='answer'),
]
-------------------------------------------------
views.py
def index(request):
requests.post(example.com, data='My data')
return redirect('page_2')
def page_2(request):
# wait request in answer
if request.session['answer'] is True:
return 'Ok'
retunr 'Bad'
def answer(request):
data = request.data
# send to page_2 or save in request.session['answer']
return Response(status=200)
Tell me with what you can wait for a response to another endpoint?
I am on the main page (index), entering something into the form. The POST request is sent to another server. At this moment:
- another server processes the data and, depending on their correctness, makes a POST request to my url /answer (True or False).
- I will be redirected, for example, to another page.
How to register the logic of another page (another) so that Django waits for a POST request from another server to /answer and depending on this request True/False, I output everything OK or everything Bad on this page?
url.py
urlpatterns = [
path('index/', index, name='index'),
path('page_2/', page_2, name='page_2'),
path('answer/', answer, name='answer'),
]
-------------------------------------------------
views.py
def index(request):
requests.post(example.com, data='My data')
return redirect('page_2')
def page_2(request):
# wait request in answer
if request.session['answer'] is True:
return 'Ok'
retunr 'Bad'
def answer(request):
data = request.data
# send to page_2 or save in request.session['answer']
return Response(status=200)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为这是一种奇怪的情况,如果您可以重新设计代码逻辑,以便视图函数尽快处理请求,而不是忙于等待外部事件被触发,因为它会增加响应时间,那就更好了。
但是,为了实现此目的,我们需要在
index
和answer
视图之间建立通信通道。因此,要实现这样的通信:index
:嘿answer
!我已发送请求。我要睡觉了,等结果出来了就叫醒我。answer
:噢,我明白了。给你。醒醒吧!索引
:谢谢。现在我处理它并返回我的回复。所以这个频道可能是任何东西!数据库中的模型、redis 中的一些实体、文件系统中的一些文件等。
使用模型的一种可能的解决方案可能是:
ExampleRequest
),由名为 < 的布尔字段组成code>receivedindex
视图中,在发送请求之前使用received = False
创建一个ExampleRequest
实例。answer
视图中,找到之前创建的ExampleRequest
并将其received
字段设置为True
index< /code> 查看,发送请求后,在 while 循环中,查询数据库,检查创建的
ExampleRequest
实例是否有received = True
?如果是,则外部服务器已调用answer
。所以,休息一下,做剩下的工作;否则,只需time.sleep(1)
并继续 while 循环。请注意:
index
视图,然后就会有多个ExampleRequest
实例。在answer
视图中,您必须能够找出当前请求与其中哪个实例相关。您可能需要在ExampleRequest
模型中存储与该请求相关的唯一数据。answer
view 的情况。因此,index
视图的 while 循环的迭代可能存在上限。index
视图中捕获ExampleRequest
实例后将其删除,以优化数据库的磁盘使用。我再说一遍,如果您可以在前端而不是后端进行轮询,以避免高响应时间和其他同步问题,那就更好了。
I reckon it's a strange situation and it's better if you could redesign the logic of your code so that the view functions process the request ASAP and not busily wait for external events to be triggered as it increases response time.
However, in order to achieve this purpose we need a communication channel between
index
andanswer
view. So to implement a communication like this:index
: Heyanswer
! I've sent the request. I'm going to sleep, wake me up if you got its result.answer
: Oh I got it man. Here you are. Wake up!index
: Thanks. Now I process it and return my response.So this channel might be anything! A model in database, some entities in redis, some files in filesystem, etc.
One possible solution using the models might be:
ExampleRequest
for example) consisting of a boolean field namedreceived
index
view, create an instance ofExampleRequest
withreceived = False
before sending the request.answer
view, find the previously createdExampleRequest
and set itsreceived
field toTrue
index
view, after sending the request, in a while loop, query the database and check if the createdExampleRequest
instance hasreceived = True
? If yes, then the external server has calledanswer
. So break the while and do the rest of the work; otherwise, justtime.sleep(1)
and continue the while loop.Just note:
index
view and then there will be more than one instance ofExampleRequest
. Inanswer
view, you have to be able to find out the current request is related to which one of those instances. You might need to store a unique data related to that request inExampleRequest
model.answer
view ever. So there might be an upper bound for the iterations ofindex
view's while loop.ExampleRequest
instances after capturing them inindex
view in order to optimize disk usage of your database.I say it again, it's better if you can do the polling stuff in frontend instead of backend to avoid high response time and other syncing issues.
这可能不是完整的答案,但它为您提供了方法。
改为如下
This might not the complete answer, but it gives you way.
Change it to following