簡體   English   中英

Django psycopg2 模板返回

[英]Django psycopg2 template return

i am searching some words in remote database with python psycopg2 module and than append them in list then taking it django template.. i have problem with how can i get third and eight object from list

我的觀點.py:

result=[]
....
rows = cur.fetchall()
for row in rows:
   result.append(row)

return render_to_response("search/se.html",{"data":"res":result}})

在我的 se.html

{% for ress in data.res %}
      {{ ress }}
{% endfor %}

在這里,我從我的數據庫中獲取所有搜索的行列,但我想要作為 django 過濾器查詢集結果

{{ ress.id }}
{{ ress.name }}

謝謝

抱歉誤讀了這個問題。 嘗試從這里開始

def fields(cursor):
    results = {}
    column = 0
    for d in cursor.description:
        results[d[0]] = column
        column = column + 1

    return results 

如您所見,您可以修改我的代碼以列出您的第 3 列和第 8 列

這個片段也可能對你有幫助

這是我的第一個答案。 希望是對的^^

而不是 for 循環,我會使用 while 循環。

result1 = []
result2 = []

rows = cur.fetchone()
while rows:
     result1.append(row[2])    # takes all third objects of the list
     result2.append(row[7])    # takes all eight .....
     rows = cur.fetchone()

這適用於正常的 Python。 我也希望在 Django 中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM