Tell me if there is a ready-made algorithm, according to the following task, otherwise I myself did not understand how to os.listdir("/news") through the while list of articles obtained by the os.listdir("/news") method, os.listdir("/news") don’t understand how to substitute the values ​​obtained in the cycle as pages. There is a news folder ( /news ) in this folder of the html page ( 1.html , 2.html , etc.).
Home ( index.cgi ) is a cgi python script. C content of the form:

 print """ ........ <div id="news1"> %s </div> <div id="news2"> %s</div> <div id="news3> %s </div> <div id="nav_news"> %s </div> ......... """ % (last_news, last_news-1, last_news-2, links) 

It is necessary to display the last three news from the folder on the main page (for example, if there is news 8, then the first news is the content of the page 8.html , the second news is the content of the page 7.html , the 3rd news is the content of the page 6.html ). Next, generate a link to the second page (for the div block with id="nav_news" ) and place news from the 5.html , 4.html , 3.html files on the second page. Next, generate a link to the third page and place news on it in div blocks with id="news1" and news2 - 2.html and 1.html respectively. And so on generate links depending on how much news in the folder.

    1 answer 1

     import cgi, os def generatelinks(page): htmls = os.listdir("/news") htmls.reverse() pages = htmls[(page-1)*3:(page-1)*3+3] while len(pages)<3: pages.append("") print """ ........ <div id="news1"> %s </div> <div id="news2"> %s </div> <div id="news3"> %s </div> <div id="nav_news"><a href="http://localhost:8000/cgi-bin/index.py?page=%s"> Next page </a></div> ........ """ % (pages[0], pages[1], pages[2], page+1) form = cgi.FieldStorage() page = form.getvalue("page", "1") page = int(page) generatelinks(page) 
    • @ dinar-gataulin, </ html> "" ".format (* pages, page + 1)) SyntaxError: invalid syntax on python 2.7.9 server - Sergey White
    • Changed list unpacking. Check now. - Dinar Gataullin
    • @ dinar-gatulin, the same SyntaxError: invalid syntax though ... now I tried with a computer there just in the news sections {0} of the news2 block is {1} ... Although I already managed to return to the original view on the server ... strangely somehow - Sergey White
    • but. Well this is because there are links to .format ( - Sergey White
    • one
      @ dinar-gatulin, Thanks a lot! It works, but only does not display the contents of the page, but prints its name ... tried to do <?php include %s ?> But for some reason it does not load. Besides reading the contents of the method pages[0].read() are there other options? And I still don’t understand what is happening here regarding the page variable. It is not set anywhere. Well, that is, to get it from form.getvalue("page", 1) , it had to be transferred from the form or else how, but nowhere is it transmitted - Sergey White