Django模板视图类无法正常工作

时间:2015-01-26 07:08:38

标签: python django django-templates django-class-based-views

我是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

1 个答案:

答案 0 :(得分:1)

问题在于 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