Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用AJAX通过用户输入从数据库检索记录_Python_Ajax_Google App Engine_Google Cloud Sql - Fatal编程技术网

Python 使用AJAX通过用户输入从数据库检索记录

Python 使用AJAX通过用户输入从数据库检索记录,python,ajax,google-app-engine,google-cloud-sql,Python,Ajax,Google App Engine,Google Cloud Sql,平台:谷歌应用程序引擎(Python) 数据库:谷歌云SQL 我基本上有一个带有侧边栏和主视图的简单网页。侧边栏基本上由数据库中的一组记录组成,当我单击其中一个记录时,我希望从数据库中提取更详细的信息并显示在主视图中 我尝试在处理程序中使用def get(self),但是没有读取变量。是否可以在def get(self)中使用self.request.get('variable')?是否有其他方法将变量从def post(self)传递到def get(self) 下面是我正在使用的大部分代码。

平台:谷歌应用程序引擎(Python) 数据库:谷歌云SQL

我基本上有一个带有侧边栏和主视图的简单网页。侧边栏基本上由数据库中的一组记录组成,当我单击其中一个记录时,我希望从数据库中提取更详细的信息并显示在主视图中

我尝试在处理程序中使用def get(self),但是没有读取变量。是否可以在def get(self)中使用self.request.get('variable')?是否有其他方法将变量从def post(self)传递到def get(self)

下面是我正在使用的大部分代码。除了下面的代码外,我还将结果传递给模板,然后使用AJAX再次获取模板。那部分工作正常

//HTML代码

<table class="query-results user">
<tbody>
{% autoescape on %}
{% for row in user_queries %}
<tr class="query-results">
  <td class="query-user">
    <img class="query-user-avatar" src="../img/avatars/{{ row.1 }}_32x32x32.png"><br />
  </td>
  <td class="query-main" data-queryid="{{ row.8 }}">
    <span class="query-title">{{ row.2 }}</span><br />{{ row.3 }}</br>{{ row.0 }} || {{ row.5 }} ago
  </td>
</tr>
{% endfor %}
{% endautoescape %}
</tbody>
</table> 
Python代码:

$("td.query-main").live('click', function(){
  var queryid = $(this).data("queryid");
    var dataString = 'queryId='+ queryid;
    $.ajax({  
      type: "GET",  
      url: "/querytab",  
      data: dataString,
      success: function () { alert("data sent: " + dataString) }
      });  
    return false;
});
class QueryTab(webapp2.RequestHandler):
  def get(self):
    query_id = self.request.get('queryId')

    fmt = '%Y-%m-%d %H:%M:%S'

    conn = rdbms.connect(instance=_INSTANCE_NAME, database='userPrefs')
    cursor = conn.cursor()
    cursor.execute('SELECT q.userNickname, a.avatar, q.queryName, q.queryDescription, q.queryValue, q.dateCreated, q.dateLastUpdated, q.activeFlag, q.uniqueId, q.entryID FROM queries AS q LEFT JOIN avatarPrefs AS a ON q.userNickname = a.userNickname WHERE q.activeFlag ="true" AND q.uniqueId = %s ORDER BY q.dateLastUpdated DESC LIMIT 6', (query_id))
    self_query = [[str(row[0]), str(row[1]), str(row[2]), str(row[3]), str(row[4]), str(timesince(datetime.strptime(str(row[5]), fmt))), str(timesince(datetime.strptime(str(row[6]), fmt))), str(row[7]), str(row[8]), str(row[9])] for row in cursor.fetchall()]

    template_file_name = 'templates/querytab.html'
    template_values = {
      'self_query': self_query,
      'query_id': query_id,
    }

    path = os.path.join(os.path.dirname(__file__), template_file_name)
    self.response.out.write(template.render(path, template_values))
    conn.close()

当时,我还不太熟悉AJAX,所以不太了解回调功能。我最终使用的是以下内容:

$('td.query-main').live('click', function() {
  var queryid = $(this).data("queryid");
  var dataString = 'term=' + queryid;
  $.ajax({  
    type: "POST",  
    url: "/snippets",  
    data: dataString,
    success: function(data) {
      $('#query-focus').html(data);
    }
  });
});

进行一些调试,看看哪里出了问题。Ajax是否正确地从DOM获取queryid?它是否正确地将其发送到请求中?Python在request.get中看到了什么吗?等等。我添加了一些调试日志,似乎正确地接收到了queryId。查询也起作用(使用logging.info,我能够检索到这些值)。然而,奇怪的是,由于某种原因,模板无法提取它们,并向我抛出以下消息:
query\u info=self\u query[0]indexer:list index out out range
,这很奇怪,因为logging.info在理解这一点时没有任何问题。。。