3

我有一个 Django 网站,它根据用户选择显示不同的甘特图。我的代码中有一堆 Django 的日志消息,并且希望能够在用户点击提交按钮后设置并显示在弹出窗口中,但我似乎无法让消息显示在弹出窗口中。

我的views.py 看起来像这样:

from django.contrib import messages

def gantt_search(request):

  if request.method == 'POST':
    messages.info(request,"Query submitted, beginning query results render for:")

    form_start = WINDOW_START_Form(request.POST,section_label="WINDOW_START")

    if form_start.is_valid():
        WS_raw = form_start.cleaned_data['WINDOW_START']
        messages.info(request,"Window Start time: {WS}".format(WS=WS_raw))

    else:
        messages.error(request,"The start date you entered is incorrectly formatted.")

  else:
    messages.info(request,"Rendering query page")
    form_start = WINDOW_START_Form(section_label="WINDOW_START")

  return render(request, 'InterfaceApp/table_search.html', {'window_start': form_start})

def msgs(request):
   m = messages.get_messages(request)
   return render_to_response('InterfaceApp/messages.html',{'message':m})

结果模板包含我必须显示弹出链接的代码,如下所示:

<html>
  <head>
    <title>Search Results</title>
    <meta charset="utf-8">
    <script src="http://code.jquery.com/jquery-latest.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script language="javascript" type="text/javascript">
      function popitup(url) {
          newwindow=window.open(url,'{{title}}','height=200,width=150');
          if (window.focus) {newwindow.focus()}
          return false;
      }
    </script>
  </head>
  <body>
    <div class="container">
      <a href="messages.html" onclick="return popitup('/InterfaceApp/msgs')" > {% trans 'View Log Messages' %}
      </a>
    </div>
  </body>
</html>

messages.html 模板如下所示:

<body>
 <div class="container">
  <h2>Messages</h2>
  {{ message }}
 </div>
 <div class="containter">
  {% if messages %}
  <ul class="messages">
      {% for message in messages %}
      <li{% if message.tags %} class="{{ message.tags }}"{% endif %}>
          {% if message.level == DEFAULT_MESSAGE_LEVELS.DEBUG %}Important: {% endif %}
          {{ message }}
      </li>
      {% endfor %}
  </ul>
  {% endif %}
 </div>
</body>

弹出窗口工作正常,但是当它打开时,我看到<django.contrib.messages.storage.fallback.FallbackStorage object at 0x10965bdd0>的不是消息列表。消息在呈现时确实正确显示在我的结果页面的顶部,但我不希望它们在那里。

谁能告诉我如何解决这个问题?

谢谢!

4

0 回答 0