从包含标签内延迟显示消息 [英] Delayed display of message from within an inclusion tag

查看:339
本文介绍了从包含标签内延迟显示消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在大型django项目中出现了以下问题。我已经能够在一个小型的实例项目(下面的代码)中复制这个问题。

The following problem is occurring in a large django project. I've been able to replicate the issue in a small mock-up project (code below).

我试图使用 django message framework 内的包含标签在POST的表单返回时显示消息 is_valid()。此方法也用于另一个答案此处(请参阅最终更新部分)。

I am trying to use the django messaging framework within an inclusion tag to display a message when a POST'ed form returns is_valid(). This approach has also been used in an another answer here (see 'final update' section).

问题是在POST之后呈现页面时不会立即显示该消息。相反,消息会显示在下一个时间,您可以浏览其他地方,或者在收到POST响应后刷新页面

The problem is that the message is not immediately displayed when the page is rendered after the POST. Instead the message appears the next time you navigate elsewhere or refresh the page after the POST response is received.

我没有收到任何错误。一切似乎正常运行,除了延迟的消息显示。

I am not receiving any errors. Everything appears to be operating normally, except for the delayed message display.

这种方法的原因是因为我在多个应用程序中重复使用多个小表单,我需要使用DRY主体作为GET和POST逻辑。这种方法是完美的 - 除了延迟的成功消息显示的问题!

The reason for this approach is because I'm reusing multiple small forms across multiple apps and I need to use DRY principals for the GET and POST logic. This approach works perfectly - except for the issue with the delayed 'success' message display!

真的感谢任何反馈或帮助!

Really appreciate any feedback or assistance!

编辑:要清除在'my_template.py'中设置消息的行:

To be clear the line which sets the message is in 'my_template.py':

messages.add_message(context['request'], messages.SUCCESS, "Successfully added entry")

演示项目

settings.py:

settings.py:

...
    TEMPLATE_CONTEXT_PROCESSORS = (
       "django.core.context_processors.request",
       "django.core.context_processors.media",
       "django.contrib.messages.context_processors.messages"
    )
...

base_layout.html:

base_layout.html:

<!DOCTYPE html>
<html>
<head>
    <title>Test</title>
</head>
<body>
{% for message in messages %}<div class="alert{% if message.tags %} alert-{{ message.tags }}{% endif %}" role="alert">{{ message }}</div>{% endfor %}
{% block content %}{% endblock %}
</body>
</html>

my_template.html:

my_template.html:

<form action="" method="post">
    {% csrf_token %}
    {{ form }}
    <input type="submit" value="Submit" />
</form>

forms.py:

from django.forms.models import ModelForm
from app.models import ContactMessage

class ContactForm(ModelForm):
    class Meta:
        model = ContactMessage
        fields = ['name']

index.html:

index.html:

{% extends "app/base_layout.html" %}
{% load my_template %}
{% block content %}
{% my_template %}
{% endblock %}

从django导入模板
从django.contrib导入消息
从app.forms


my_template.py:

from django import template
from django.contrib import messages
from app.forms import ContactForm
register = template.Library()


@register.inclusion_tag('app/my_template.html', takes_context=True)
def my_template(context):

    if context['request'].method=='GET':
        return { 'form':ContactForm() }

    if context['request'].method=='POST':
        form = ContactForm(context['request'].POST)
        if not form.is_valid():
            return { 'form': form }

        form.save()

        messages.add_message(context['request'], messages.SUCCESS, "Successfully added entry")
        return { 'form':ContactForm() }


推荐答案

根据Django,消息排队等待呈现被渲染器清除。 (参考)

According to Django, the messages are queued for rendering until it is cleared by renderer. (reference)

在您的情况下,您正在添加消息,在$ code> {{message}} 中已经呈现了base.html中的标签。所以你的消息被存储直到你的下一个视图,当$ code> {{message}} 在base.html再次被渲染。

In your case, you are adding messages after {{ message }} tags in base.html has been rendered. So your message is stored until your next view when {{ message }} in base.html is rendered again.

为了解决这个问题,您可以在 {%endblock%} 的内容之后移动 {{message}} 标签。另一种可能的解决方案是使用javascript从my_template.html或从base.html的末尾附加 {{message}} 标签。

To solve this, you can move your {{ message }} tags behind {% endblock %} of content. Another possible solution is to use javascript to append {{ message }} tags either from my_template.html or from end of base.html.

这篇关于从包含标签内延迟显示消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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