python - 将 javascript 添加到 Django 重定向 (HttpResponse)

标签 python django redirect httpresponse

我在 Django 中使用重定向 from django.shortcuts import redirect

我想要的是在重定向之前向用户显示 JavaScript 警报消息。

这是我到目前为止所得到的:

response = redirect("someUrl")
response.write('<script>alert(\'You must remove an item before adding another\');</script>')
response['location'] += "?active=" + "all"
return response

重定向有效,但 Javascript 无效。我也尝试过

response = HttpResponse('<script>alert(\'You must remove an item before adding another\');</script>')
return response

这会触发警报窗口,但我不确定如何添加与上述方法等效的重定向。

最佳答案

您可以使用js方法 window.location

url = 'someUrl'
resp_body = '<script>alert("You must remove an item before adding another");\
             window.location="%s"</script>' % url
return HttpResponse(resp_body)

关于python - 将 javascript 添加到 Django 重定向 (HttpResponse),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39414365/

相关文章:

python - 如何以json格式发送jwt token ?

python - manage.py 抛出错误 "PostgreSQL with tsearch2 support is needed to use the pgsql FTS backend"

perl - 如何确定 URL 是否重定向?

jquery - 从 ajax Spring MVC Controller 方法执行正常重定向

regex - nginx 使用 OR 在正则表达式中设置 map

python - 使用 Seaborn 和 Matplotlib 对齐热图和线图的共享子图中的 x 轴刻度

python - 在 python 中以非标准表示法表达 float ?

Python Wand 将 tiff 更改为 min-is-white

python - Django REST Framework 验证 slug 字段

python - Google App Engine 中是否有等效的 Django 应用程序模式?