Django如何在post请求结果之后重定向

2024-09-30 08:37:43 发布

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

def postDataRequest(request):
        if request.method == 'POST':
            username = request.POST['username']
            userid= request.POST['userid']
            if(userid== "21310"):
                context = {
                    "result" : Dataengine(username,userid).dataFunc()
                }
                messages.add_message(request, messages.SUCCESS, context)
                return render(request, 'pages/dataresult.html', context)
                   

我通过post请求向用户显示一些数据。我要做的是在用户在'dataresult.html'页面上看到结果后30秒内重定向'pages/index.html'。我该怎么做


Tags: 用户ifrequestdefhtmlcontextusernamepages
2条回答

您可以使用返回重定向(“app:view”)它会将您重定向到下一个视图,如果您想使用上下文值重定向,则可以

1.from django.http导入HttpResponseRedirect

返回HttpResponseRedirect(反向('app:view',context))

2。重定向(反向('app:view',context))

因为在django中,不能使用重定向传递参数。您唯一的赌注是将它们作为URL的一部分传递

在模板中,您可以使用以下命令将页面重定向到给定URL:

<meta http-equiv="refresh" content="5; URL={% url 'name-of-index-view' %}">
<link rel="canonical" href="{% url 'name-of-index-view' %}">

通过将其添加到模板中的<head>隔室中,该隔室由使用dataresult.htmlURL激发的视图呈现

相关问题 更多 >

    热门问题