Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用上下文要求的Django确认表单重新提交_Django - Fatal编程技术网

使用上下文要求的Django确认表单重新提交

使用上下文要求的Django确认表单重新提交,django,Django,我有一个帐户页面,允许用户编辑他们的帐户。按下保存按钮时,我运行以下代码: request.user.first_name = request.POST['first_name'] request.user.last_name = request.POST['last_name'] request.user.save() context['alert'] = { 'title': "Your information has been updated", 'c

我有一个帐户页面,允许用户编辑他们的帐户。按下保存按钮时,我运行以下代码:

request.user.first_name = request.POST['first_name']
request.user.last_name = request.POST['last_name']
request.user.save()

context['alert'] = {
    'title': "Your information has been updated",
    'color': 'primary',
}
如您所见,我在上下文中传递,在我的模板中,该上下文作为警报呈现。但是,当我刷新页面时,它会显示:
确认表单重新提交


我怎样才能消除这个错误?谢谢

成功的POST请求通常会导致重定向,这就是所谓的

为了向用户发送消息,您可以使用。因此,您可以为用户添加消息,然后在其他视图中呈现该消息:

从django.shortcuts导入重定向
从django.contrib导入消息
定义我的视图(请求):
request.user.first\u name=request.POST['first\u name']
request.user.last_name=request.POST['last_name']
request.user.save()
messages.success(请求“您的信息已更新”)

return redirect('name-of-some-view')
成功的POST请求通常会导致重定向,这就是所谓的重定向

为了向用户发送消息,您可以使用。因此,您可以为用户添加消息,然后在其他视图中呈现该消息:

从django.shortcuts导入重定向
从django.contrib导入消息
定义我的视图(请求):
request.user.first\u name=request.POST['first\u name']
request.user.last_name=request.POST['last_name']
request.user.save()
messages.success(请求“您的信息已更新”)

return redirect('name-of-some-view')
一个通常执行重定向,这是Post/redirect/Get模式:一个通常执行重定向,这是Post/redirect/Get模式:感谢您的回复!!如何解析模板中的messages.success()?@JJT:请参阅。通常,最好将其包含在“基本模板”中,这样您就可以在所有页面上呈现消息。感谢您的回复!!如何解析模板中的messages.success()?@JJT:请参阅。通常,最好将其包含在“基本模板”中,以便可以在所有页面上呈现消息。
from django.shortcuts import redirect
from django.contrib import messages

def my_view(request):
    request.user.first_name = request.POST['first_name']
    request.user.last_name = request.POST['last_name']
    request.user.save()
    messages.success(request, 'Your information has been updated')
    return redirect('name-of-some-view')