gpt4 book ai didi

HttpResponseRedirect后未显示Django消息

转载 作者:行者123 更新时间:2023-12-04 09:59:03 24 4
gpt4 key购买 nike

尝试了很多东西,但没有一个真正有用。

我有一个URL例如:

http://localhost:8000/user/edit-transaction/?object_id=23a959d0561711e59e36acd1b8679265&type=grossary

调用下面的view:

def edit_transaction(request):

if request.method == "POST":
if something is True:
messages.error(request, 'Error message here')

# this don't work
return HttpResponseRedirect(request.META.get('HTTP_REFERER'))

# but this work
template = "user/xyz/abc.html"
render(request, template)
else:
return HttpResponseNotFound()
else:
context = {
'key1': 'value1',
'key2': 'value2',
}
template = "user/xyz/abc.html"
render(request, template, context)

在模板内部:
{% if messages %}

<h1>I am inside MESSAGES</h1>

{% for message in messages %}
{% if message.tags == 'success' %}
<div class="alert alert-success" role="alert">{{ message|escape|safe }}</div>
{% elif message.tags == 'error' %}
<div class="alert alert-danger" role="alert">{{ message|escape|safe }}</div>
{% endif %}
{% endfor %}
{% endif %}

它进入了 ifif something is True:内部,并与查询字符串一起重定向到了同一页面。但不显示错误消息。

我想要的是重定向到同一页,保留查询字符串并显示错误消息。我在这里做错了什么,建议进行哪些更改(如果有的话)。

Also a doubt, that does Django messages really work after redirection like Flash messages should ??



编辑:

1)无效:
if something is True:
messages.error(request, 'Error message here')
return HttpResponseRedirect(request.META.get('HTTP_REFERER'))

从控制台:

[16/Sep/2015 10:57:08]"POST /user/edit-transaction/?object_id=23a959d0561711e59e36acd1b8679265&type=grossary HTTP/1.1" 302 0 [16/Sep/2015 10:57:08]"GET /user/edit-transaction/?object_id=23a959d0561711e59e36acd1b8679265&type=grossary HTTP/1.1" 200 8832



2)可行:
if something is True:
messages.error(request, 'Error message here')
template = "user/xyz/abc.html"
render(request, template)

从控制台:

[16/Sep/2015 10:57:08]"POST /user/edit-transaction/?object_id=23a959d0561711e59e36acd1b8679265&type=grossary HTTP/1.1" 302 0



因此,从上面我基本上了解到, messages随着附加请求而过期(重定向200)。

而且在模板中,打印 {% if messages %}也不会进入 <h1>I am inside MESSAGES</h1>

最佳答案

终于想通了。在local_settings.py及其工作中添加以下内容。

MESSAGE_STORAGE = 'django.contrib.messages.storage.session.SessionStorage'

发生了什么事?

消息实际上已存储在 Cookies (CookieStorage)中,这是默认的Django行为。从 Django docs:

FallbackStorage is the default storage class. If it isn’t suitable to your needs, you can select another storage class by setting MESSAGE_STORAGE to its full import path, for example:

MESSAGE_STORAGE = 'django.contrib.messages.storage.cookie.CookieStorage'



什么是FallbackStorage?

This class first uses CookieStorage, and falls back to using SessionStorage for the messages that could not fit in a single cookie. It also requires Django’s contrib.sessions application.

This behavior avoids writing to the session whenever possible. It should provide the best performance in the general case.



我的情况如何?
Messages正在存储在 CookiesStorage中,但是出于某些奇怪的原因(我不知道是什么),但是 Messages中的 CookiesStorage对于第二个请求(即POST之后的重定向)已过期或删除了,这应该不会发生(因为这不是 flashdata的工作原理)。在将默认的 MESSAGE_STORAGE切换为 SessionStorage之后,它开始工作。

关于HttpResponseRedirect后未显示Django消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32600473/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com