Django 模板视图 class 不工作

Django Template view class not working

我是 django 的新手,想使用基于分类的视图将一些数据传递到我的模板。为此,我使用了模板视图。但无法获得成功。

可见

class MessagesSummaryView(generic.TemplateView):
    template_name = 'message_history.html'
    log_dict = {}

    def get_context_data(self, **kwargs):
        context = super(MessagesSummaryView, self).get_context_data(**kwargs)
        cur = connection.cursor()
        cur.execute("SELECT logmsg.message_id, COUNT(logmsg.log_id) AS TotalUsers, logmsg.sent_on AS Sent_ON, logmsg.log_status, msg.message_subject FROM sendmessage_log_messages logmsg, sendmessage_messages msg WHERE from_name = '"+ str(request.user.email) +"' and  logmsg.message_id = msg.message_id  GROUP BY logmsg.message_id, logmsg.sent_on, logmsg.log_status, msg.message_subject;")
        rows = cur.fetchall()
        count = 0
        for row in rows:
            temp={}
            temp['message_id'] = row[0]
            temp['count'] = row[1]
            temp['Sent_ON'] = str(row[2])
            temp['message_subject'] = row[3]
            log_dict[row[0]] = temp
        context['message_history'] = self.log_dict
        return context

并在模板中

  <table id="mytable" class="table table-bordred table-style-two" style="table-layout:auto; border:black solid thin;">
                <tbody>
                  <div id="content">
                    {% for contact in message_history %}
                    <tr>
                      <!--   <td style="text-align:center;">{{ contact.list_id }}</td> -->
                      <td style="text-align:left; font-size:small;  width:10%;">You Had Send Message : {{ contact.message_subject }} to {{ contact.TotalUsers }} on {{ contact.sent_on }}. <a href="/message/?message_id={{ contact.message_id}}">Show Detials</a></td>
                    </tr>
                    {% endfor %}
                  </div>
                </tbody>
              </table>

解决了很多错误,但是页面上没有显示数据,一个是

global name 'request' is not defined

问题出在线路上 cur.execute("SELECT logmsg.message_id, COUNT(logmsg.log_id) AS TotalUsers, logmsg.sent_on AS Sent_ON, logmsg.log_status, msg.message_subject FROM sendmessage_log_messages logmsg, sendmessage_messages msg WHERE from_name = '"+ str(request.user.email) +"' and logmsg.message_id = msg.message_id GROUP BY logmsg.message_id, logmsg.sent_on, logmsg.log_status, msg.message_subject;") 您尝试使用 request.user.email.

的位置

请尝试使用 self.request.user.email