Django消息框架消息不让它通过redirect() [英] Django messaging framework messages not making it passed the redirect()

查看:246
本文介绍了Django消息框架消息不让它通过redirect()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些保存代码,在成功创建记录之后,使用Django消息传递框架设置成功消息,然后将重定向重定向到主要的application_update视图。我无法确定为什么这些邮件不会通过重定向。在执行render_to_response时,消息似乎工作正常,但是在执行redirect时不会。

I have some "save" code that sets a success message using the Django messaging framework upon successful creation of a record, then does a redirect back to the main 'application_update' view. I'm having trouble determining why these messages are not making it thru the redirect. The messaging seems to work just fine when doing a "render_to_response", but not when doing a "redirect".

在views.py中的函数片段(如果POST):

function snippet (if POST) in views.py:

if ovrd_form.is_valid():
    fields = {'application': ovrd_form.cleaned_data['application'],
              'course': ovrd_form.cleaned_data['course'],
              * other field/values *
             }
    try:
        overrides = Overrides(**fields)
        overrides.save()
        success_msg = 'Override creation was successful.'
        create_message(request, success_msg, 'success')
    except Exception, exception:
        return HttpResponse('Error: ' + str(exception))

    return redirect('application_update', app_id=app_id)

create_message()函数:

create_message() function:

from django.contrib import messages

def create_message(request, msg, msg_type):
    """ build a message & sets the correct message.type """
    if msg_type == 'error':
        django.contrib.messages.error(request, msg)
    elif msg_type == 'warning':
        django.contrib.messages.warning(request, msg)
    elif msg_type == 'success':
        django.contrib.messages.success(request, msg)
    elif msg_type == 'info':
        django.contrib.messages.info(request, msg)
    elif msg_type == 'debug':
        django.contrib.messages.debug(request, msg)

所有模板都继承了这段代码:

all templates inherit this piece of code:

{% if messages %}
    {% for message in messages %}
        {% comment %}force -danger if error type for bootstrap css class{% endcomment %}
        {% if message.tags == 'error' %}
            <div class="alert alert-danger">
        {% else %}
            <div class="alert alert-{{ message.tags }}">
        {% endif %}
            <button type="button" class="close" data-dismiss="alert">&times;</button>
            <span>{{ message }}</span>
            </div>
    {% endfor %}
{% endif %}

任何帮助非常感谢。

推荐答案

尤里卡!有点。看来我可能需要重新启动我的虚拟机会话(看不到这是怎么回事,但陌生的事情),或者我需要清除我的浏览器缓存(会话协议)。

Eureka! Sort of. It would seem that I may have either needed to reboot my VM session (can’t see how this would be it, but stranger things…) or I needed to clear my browser cache (session parms).

重新启动虚拟机:不时会遇到我的Vagrant VM Box的内部时钟问题,导致Oauth差异,从而导致我的REST API调用失败,一旦我重新启动SSH会话和/或流氓停止并移动我的虚拟机,它被更正。这如何与我的消息传递问题相结合?不知道它是什么,只是说我可能会在一段时间内得到一些虚拟机损坏,这可能会影响到我所知道的更多。为我学习的教训是重新启动虚拟机,只要不奇怪,只是不加起来。

Reboot of VM: from time to time I experience internal clock issues with my Vagrant VM box which results in Oauth discrepancies, which in turn causes my REST API calls to fail, which is corrected once I reboot the SSH session and/or vagrant halt and vagrant up my VM. How does this tie in to my messaging problem? Not sure it does, only stating that I may be getting some VM corruption over time that could affect more than what I’m aware of. Lesson learned for me is to reboot the VM anytime I have weirdness that just doesn’t add up.


  • 注意:我每天晚上都不关机我的虚拟机或电脑,我把它放回家。这意味着它必须与不同的wifi网络联系,这就导致我必须经常重启我的SSH终端。虚拟机因此运行了几天...或至少直到它变得越来越麻烦。

清除我的缓存:我在周末忘了这个问题,当我今天早上开始工作的时候... walla!一切都很好。我试图重新加载页面(以获得auths问题修复)之前,必须做上述虚拟机的事情,所以我不确定如果修正来自于这个,或只是因为我的会话parms可能已过期。

Clear my cache: I made myself forget about this problem over the weekend and when I started working on it this morning…walla! All is well. I had to do the VM thing stated above before trying to reload the page (to get the auths problem fixed), so I’m not certain if the correction came from that or just because my session parms likely expired.

感谢@TravisD& @DanielRoseman的帮助!

Thanks to @TravisD & @DanielRoseman for the help!

这篇关于Django消息框架消息不让它通过redirect()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆