【问题标题】:Displaying a message as a popup/alert in Django?在Django中将消息显示为弹出/警报?
【发布时间】:2022-01-25 00:43:34
【问题描述】:

我有一个表单,提交后它会再次重定向到自身。我正在尝试显示一条消息“您已进入 (work_area)”。目前,我只得到了没有变量的显示部分,但它在用户提交后显示在 html 中。我希望它是一个弹出窗口或警告消息,用户可以在看到它后关闭它,但我不明白这在 Django 中是如何工作的。

views.py

class EnterExitArea(CreateView):
    model = EmployeeWorkAreaLog
    template_name = "operations/enter_exit_area.html"
    form_class = WarehouseForm
    #success_message = "You have logged into %(work_area)s"

    def form_valid(self, form):
        emp_num = form.cleaned_data['employee_number']
        area = form.cleaned_data['work_area']
        station = form.cleaned_data['station_number']

        if 'enter_area' in self.request.POST:
            form.save()
            EmployeeWorkAreaLog.objects.filter((Q(employee_number=emp_num) & Q(work_area=area) & Q(station_number=station)).update(time_in=datetime.now())

            messages.info(self.request, "You have entered")
            return HttpResponseRedirect(self.request.path_info)

enter_exit_area.html

{% extends "base.html" %}

{% block main %}
    <form id="warehouseForm" action="" method="POST" class="form-horizontal" novalidate >
        {% csrf_token %}

        {% if messages %}
            <ul class="messages">
            {% for message in messages %}
                <li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
            {% endfor %}
            </ul>
        {% endif %}

        <div>
            <div>
                <label>Employee</label>
                {{ form.employee_number }}
            </div>

            <div>
                <label>Work Area</label>
                {{ form.work_area }}
            </div>
            <div>
                <label>Station</label>
                {{ form.station_number }}
            </div>
        </div>

        <div>
            <div>
                <button type="submit" name="enter_area" value="Enter">Enter Area</button>

            </div>
        </div>
    </form>

{% endblock main %}

它显示如下:

编辑:

【问题讨论】:

  • 您可能想查看 jQuery 对话框或引导模式,或者只是一个普通的 Javascript 警报。

标签: python html django popup alert


【解决方案1】:

这更像是一个 javascript 问题,但无论如何,这就是我处理这个问题的方式(如果您使用的是 bootstrap):

1) 创建一个message.html 文件,内容如下:

{% for message in messages %}
  <div class="toast notification bg-{% if message.tags == 'error' %}danger{% else %}{{message.tags}}{% endif %}" role="alert" aria-live="assertive" aria-atomic="true" data-delay="5000">
    <div class="toast-header">
      <strong class="mr-auto">
        {% if message.tags == 'error' %}
          <i class="fas fa-times mr-2"></i>
        {% elif message.tags == 'warning' %}
          <i class="fas fa-exclamation mr-2"></i>
        {% elif message.tags == 'info' %}
          <i class="fas fa-info mr-2"></i>
        {% elif message.tags == 'success' %}
          <i class="fas fa-check mr-2"></i>
        {% endif %}
        {{message.tags|capfirst}}
      </strong>
      <button type="button" class="ml-2 mb-1 close" data-dismiss="toast" aria-label="Close">
        <span aria-hidden="true">&times;</span>
      </button>
    </div>
    <div class="toast-body">
      {{message|safe}}
    </div>
  </div>
{% endfor %}

2) 更改您的base.html 并添加:

...
{% include 'message.html' %}
...


<!-- and at the end:-->
<script src="{% static 'js/jquery-3.4.1.min.js' %}"></script>
<script src="{% static 'js/bootstrap.min.js' %}"></script>
{% if messages %}
  <script>
      {% for message in messages %}
          $(document).ready(function () {
              $('.toast').toast('show');
          });
      {% endfor %}
  </script>
{% endif %}

另外,不要忘记包含引导程序。

这样您就可以在所有模板上使用 django 消息,而无需显式更改它们。

编辑

您还需要这个自定义 css 类,以便您的通知显示在页面的右上角:

.notification{
    position: absolute;
    top: 5rem;
    right: 1rem;
}

【讨论】:

  • 这把戏做得很好,我想我可能只是做了一些奇怪的事情。两个问题,我是否在 base.html 的 标签内添加脚本?而且,我收到这个警告,上面写着unresolved function or method toast() for $('.toast').toast('show');。我正在添加一张图片,说明它现在在页面上的显示方式。
  • 1) 是的,将脚本放在body 标记内 - 最后。 2)我认为您缺少引导程序的javascript 文件(bootstrap.min.js)。
  • 它以 &lt;script src="{% static "js/plugins/bootstrap.min.js" %}"&gt;&lt;/script&gt; 的形式包含在 base.html 中,但它仍然呈现出我添加到原始帖子中的图像
  • 我有这个顺序的脚本,它对我来说工作正常:1)jQuery 2)bootstrap 3)自定义脚本。也许您是在引导程序或 jQuery 之前添加自定义脚本?
  • 嗯,我有 &lt;script src="{% static "js/plugins/jquery.js" %}"&gt;&lt;/script&gt; 和我在上面添加的 标签中的引导程序,然后是最后的脚本,在 中,根据 Network,它们都成功加载
【解决方案2】:

Django 有用于消息的内置函数。

messages.success(self.request,"message sent")

确保在您的 HTML 中创建一个消息类。

{% if message.tags %} class="{{message.tags}}"{% endif %}

【讨论】:

    猜你喜欢
    • 2012-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-26
    • 2019-02-23
    • 1970-01-01
    相关资源
    最近更新 更多