Hey folks, I am new on the forum and new to Python.
I am making a news site and using python/django. My site will get all the news from the DB and fill up the template. As there is about 10 lines for the news and at some moment i may not have 10 news i want to test if that exists before send it to render, how can I do that?
Down is my code:

def teste(response):
cursor = connection.cursor()
cursor.execute("SELECT n.titulo_principal,n.desc_resumida FROM
polls_ordem_noticias o inner join polls_noticias n on
n.id_serial=o.id_noticia order by posicao")
n=cursor.fetchall()
t=get_template('Inicial.html')
c= Context({

'Titulo_Noticia_1':n[0][0],'Descricao_Noticia_1':n[0][1],'Link_Noticia_1':n[0][0],

'Titulo_Noticia_2':n[1][0],'Descricao_Noticia_2':n[1][1],'Link_Noticia_2':n[1][0]
})
return HttpResponse(t.render(c))

Hey folks, I am new on the forum and new to Python.
I am making a news site and using python/django. My site will get all the news from the DB and fill up the template. As there is about 10 lines for the news and at some moment i may not have 10 news i want to test if that exists before send it to render, how can I do that?
Down is my code:

def teste(response):
cursor = connection.cursor()
cursor.execute("SELECT n.titulo_principal,n.desc_resumida FROM
polls_ordem_noticias o inner join polls_noticias n on
n.id_serial=o.id_noticia order by posicao")	
n=cursor.fetchall()
t=get_template('Inicial.html')
c= Context({

'Titulo_Noticia_1':n[0][0],'Descricao_Noticia_1':n[0][1],'Link_Noticia_1':n[0][0],

'Titulo_Noticia_2':n[1][0],'Descricao_Noticia_2':n[1][1],'Link_Noticia_2':n[1][0]
})
return HttpResponse(t.render(c))

Use code tags for code, please. It does not run as is.

Sorry, I am just new here.
Well, I want to test if there is something in that row cause if it is missing data it generates an error, how can I do that? I think thatk would be at the lines:

'Titulo_Noticia_1':n[0][0],'Descricao_Noticia_1':n[0][1],'Link_Noticia_1':n[0][0],
 
'Titulo_Noticia_2':n[1][0],'Descricao_Noticia_2':n[1][1],'Link_Noticia_2':n[1][0]
})

Put print of values of n before those lines, post the results and properly indented Python code.

If i have only one row returning from the DB, when I try to access the page it returns an error that follows:

IndexError at /teste/

list index out of range

So I think that I gotta test the list arround here:

'Titulo_Noticia_1':n[0][0],'Descricao_Noticia_1':n[0][1],'Link_Noticia_1':n[0][0], 
'Titulo_Noticia_2':n[1][0],'Descricao_Noticia_2':n[1][1],'Link_Noticia_2':n[1][0]

I just don't have any idea of how to do that

If i have only one row returning from the DB, when I try to access the page it returns an error that follows:

IndexError at /teste/

list index out of range

So I think that I gotta test the list arround here:

'Titulo_Noticia_1':n[0][0],'Descricao_Noticia_1':n[0][1],'Link_Noticia_1':n[0][0], 
'Titulo_Noticia_2':n[1][0],'Descricao_Noticia_2':n[1][1],'Link_Noticia_2':n[1][0]

I just don't have any idea of how to do that

This article has been dead for over six months. Start a new discussion instead.