如何使用HTTP重定向传递信息(在Django中)

use*_*389 20 python django http

我有一个视图,接受表单提交并更新模型.

更新模型后,我想重定向到另一个页面,我想在此页面上显示"Field X successfully updated"等消息.

如何将此消息"传递"到另一页?HttpResponseRedirect仅接受URL.我之前在其他网站上看到过这个.这是如何完成的?

S.L*_*ott 12

这是Django的内置功能,称为"消息"

请参阅http://docs.djangoproject.com/en/dev/topics/auth/#messages

从文档:

消息与用户关联.没有过期或时间戳的概念.

成功操作后,Django管理员使用消息.例如,"民意调查Foo已成功创建." 是一条消息.

  • @Jarret和@ S.Lott错了.Django的内置消息不是基于会话的,它们是数据库支持的并且绑定到实际的DB用户对象(数据库中不存在"匿名"用户),因此它们仅可用于经过身份验证的用户.试试django-flashcookie或django-session-messages. (9认同)
  • 对匿名用户不起作用.此外,它已被弃用. (2认同)

use*_*955 8

你可以使用django-flashcookie app http://bitbucket.org/offline/django-flashcookie/wiki/Home

它可以发送多条消息并拥有无限类型的消息.假设你想要一个消息类型用于警告,一个消息类型用于错误消息,你可以写

def simple_action(request):
    ...
    request.flash['notice'] = 'Hello World'
    return HttpResponseRedirect("/")
Run Code Online (Sandbox Code Playgroud)

要么

def simple_action(request):
    ...
    request.flash['error'] = 'something wrong'
    return HttpResponseRedirect("/")
Run Code Online (Sandbox Code Playgroud)

要么

def simple_action(request):
    ...
    request.flash['notice'] = 'Hello World'
    request.flash['error'] = 'something wrong'
    return HttpResponseRedirect("/")
Run Code Online (Sandbox Code Playgroud)

甚至

def simple_action(request):
    ...
    request.flash['notice'] = 'Hello World'
    request.flash['notice'] = 'Hello World 2'
    request.flash['error'] = 'something wrong'
    request.flash['error'] = 'something wrong 2'
    return HttpResponseRedirect("/")
Run Code Online (Sandbox Code Playgroud)

然后在你的模板中显示它

{% for message in flash.notice %}
    {{ message }}
{% endfor }}
Run Code Online (Sandbox Code Playgroud)

要么

{% for message in flash.notice %}
    {{ message }}
{% endfor }}
{% for message in flash.error %}
    {{ message }}
{% endfor }}
Run Code Online (Sandbox Code Playgroud)


小智 5

我喜欢使用消息框架的想法,但是在上面的问题的上下文中,django文档中的示例对我不起作用.

让我烦恼的是django docs中的界限:

If you're using the context processor, your template should be rendered with a RequestContext. Otherwise, ensure messages is available to the template context.

这对新手来说是不可理解的(像我一样)并且需要扩展,最好是那两个选项看起来像什么.

我只能找到需要使用RequestContext进行渲染的解决方案......这不能回答上面的问题.

我相信我已经为下面的第二个选项创建了一个解决方案:

希望这会帮助别人.

== urls.py ==

from django.conf.urls.defaults import *
from views import *

urlpatterns = patterns('',
    (r'^$', main_page, { 'template_name': 'main_page.html', }, 'main_page'),
    (r'^test/$', test ),
Run Code Online (Sandbox Code Playgroud)

== viewtest.py ==

from django.contrib import messages
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse

def test(request):
    messages.success( request, 'Test successful' )
    return HttpResponseRedirect( reverse('main_page') )
Run Code Online (Sandbox Code Playgroud)

== viewmain.py ==

from django.contrib.messages import get_messages
from django.shortcuts import render_to_response

def main_page(request, template_name ):
    # create dictionary of items to be passed to the template
    c = { messages': get_messages( request ) }

    # render page
    return render_to_response( template_name, c, )
Run Code Online (Sandbox Code Playgroud)

== main_page.html ==

{% block content %}
    {% if messages %}
    <div>
        {% for message in messages %}
            <h2 class="{{message.tag}}">{{ message.message }}</h2>
        {% endfor %}
    </div>
    {% endif %}
{% endblock %}
Run Code Online (Sandbox Code Playgroud)


Ry4*_*ase 1

您可以将消息作为查询参数传递到要重定向到的 URL 吗?这不是很 RESTy,但它应该可以工作:

return HttpResponseRedirect('/polls/%s/results/?message=Updated" % p.id)
Run Code Online (Sandbox Code Playgroud)

并让该视图检查消息参数,清除其是否有问题,然后将其显示在顶部。