dudusf04 Posted November 7, 2019 at 04:22 PM Report Share #616543 Posted November 7, 2019 at 04:22 PM Boa tarde, Tenho em uma pasta vários ficheiros em txt e xml, o que eu gostaria de fazer é que esse ficheiros fossem visto em uma div de uma pagina em html. tenho um código em python e já faz a listagem destes ficheiros (testado com pycharm), só que não sei como retornar/enviar os dados. import os path = 'c:\\winpy\\' files = [] # r=root, d=directories, f = files for r, d, f in os.walk(path): for file in f: if '.txt' in file: files.append(os.path.join(r, file)) if '.xml' in file: files.append(os.path.join(r, file)) for f in files: print(f) No ficheiro html coloquei um ficheiro estático para fazer um teste e assim poder validar que o ficheiro funciona, o problema e que tenho vários txt e quero que todos sejam vistos. {% load static %} <a href="{% static 'file/10.20.100.1_hora:02-11-2019-21:00_running-config.txt' %}">teste</a> alguém pode por favor dar-me uma dica ou conselho para ultrapassar está questão? Link to comment Share on other sites More sharing options...
M6 Posted November 7, 2019 at 04:26 PM Report Share #616544 Posted November 7, 2019 at 04:26 PM Não sei se compreendi bem a tua questão. O teu script gera o html ou queres que o web server corra o teu script ou é outra coisa? 10 REM Generation 48K! 20 INPUT "URL:", A$ 30 IF A$(1 TO 4) = "HTTP" THEN PRINT "400 Bad Request": GOTO 50 40 PRINT "404 Not Found" 50 PRINT "./M6 @ Portugal a Programar." Link to comment Share on other sites More sharing options...
dudusf04 Posted November 7, 2019 at 05:26 PM Author Report Share #616546 Posted November 7, 2019 at 05:26 PM Olá M6, Desde já obrigado pela ajuda. eu quero que o web server corra o meu script. Link to comment Share on other sites More sharing options...
M6 Posted November 8, 2019 at 09:37 AM Report Share #616549 Posted November 8, 2019 at 09:37 AM Que framework web python é que estás a usar? 10 REM Generation 48K! 20 INPUT "URL:", A$ 30 IF A$(1 TO 4) = "HTTP" THEN PRINT "400 Bad Request": GOTO 50 40 PRINT "404 Not Found" 50 PRINT "./M6 @ Portugal a Programar." Link to comment Share on other sites More sharing options...
dudusf04 Posted November 11, 2019 at 10:12 AM Author Report Share #616566 Posted November 11, 2019 at 10:12 AM Django Link to comment Share on other sites More sharing options...
M6 Posted November 11, 2019 at 12:23 PM Report Share #616569 Posted November 11, 2019 at 12:23 PM Fazes a obtenção no controlador e envias a lista para a view. Na view, listas os ficheiros. 10 REM Generation 48K! 20 INPUT "URL:", A$ 30 IF A$(1 TO 4) = "HTTP" THEN PRINT "400 Bad Request": GOTO 50 40 PRINT "404 Not Found" 50 PRINT "./M6 @ Portugal a Programar." Link to comment Share on other sites More sharing options...
dudusf04 Posted November 11, 2019 at 04:24 PM Author Report Share #616573 Posted November 11, 2019 at 04:24 PM M6 creio que é isso que estou a fazer mas estou errando em algo e não consigo saber no que é. views.py from django.http import Http404, HttpResponse # # File Backup # def File_b(request): path = 'file/' files = [] # r=root, d=directories, f = files for r, d, f in os.walk(path): for fil in f: if '.txt' in fil: files.append(os.path.join(r, fil)) if '.xml' in fil: files.append(os.path.join(r, fil)) for f in files: fi = f return HttpResponse(fi) url.py # file backups url(r'file/', views.File_b, name = 'file_b'), file_backup.html {% if file_b %} {% load static %} <table class="table table-hover panel-body"> <tr> <th>Name</th> <th>Size</th> <th>Created</th> <th></th> </tr> {% for file in file_b %} <tr{% if not file.size %} class="danger"{% endif %}> <td> <i class="fa fa-image"></i>i <a href="{% static file %}" target="_blank">{{ file }}</a> </td> <td>{{ file.size|filesizeformat }}</td> <td>{{ file.created }}</td> {% endfor %} </table> {% else %} <div class="panel-body"> {% load static %} <a href="{% static file_b %}" target="_blank">{{ file_b }}</a> <!-- <a href="{% static 'file/10.20.100.1_hora:02-11-2019-21:00_running-config.txt' %}">teste</a> <span class="text-muted">None</span>--> </div> {% endif %} se puder dar um dica ou alguma indicação, sou bastante agradecido. Link to comment Share on other sites More sharing options...
M6 Posted November 11, 2019 at 04:47 PM Report Share #616574 Posted November 11, 2019 at 04:47 PM No controlador estás unicamente a juntar à lista o nome dos ficheiros (é só uma string), pelo que files.append tem strings e não identificadores de ficheiros, o que quer dizer que o file.size ou file.created não vai ter o efeito desejado na view uma vez que file é apenas uma string e não tem esses métodos. Mas antes disso, no controlador, tens um loop pela lista de files (não percebo porquê) mas que ao fim do primeiro elemento (ou seja ficheiro), fazes o render da view da terminas. Devias ter: # r=root, d=directories, f = files for r, d, f in os.walk(path): for fil in f: if '.txt' in fil: files.append(os.path.join(r, fil)) if '.xml' in fil: files.append(os.path.join(r, fil)) return HttpResponse(files) E na view deves mostar apenas o nome do ficheiro (que é a única coisa que tens). 10 REM Generation 48K! 20 INPUT "URL:", A$ 30 IF A$(1 TO 4) = "HTTP" THEN PRINT "400 Bad Request": GOTO 50 40 PRINT "404 Not Found" 50 PRINT "./M6 @ Portugal a Programar." Link to comment Share on other sites More sharing options...
dudusf04 Posted November 12, 2019 at 11:29 AM Author Report Share #616580 Posted November 12, 2019 at 11:29 AM (edited) Olá M6, Nesta parte file.size ou file.created não vai ter o efeito desejado, compreendo que não funcione. também estou a pesquisar qual é a melhor biblioteca para esta função. Realizei a alteração que aconselhaste a fazer e mesmo assim os dados não chega na variável que está no file_backup.html , o que me leva a pensar que poderá ser na url.py. Digo isso, no intuito de ainda não percebido bem o conceito da url. Edited November 12, 2019 at 11:30 AM by dudusf04 Link to comment Share on other sites More sharing options...
M6 Posted November 15, 2019 at 05:22 PM Report Share #616620 Posted November 15, 2019 at 05:22 PM O URL é o motor de redirecionamento, bastante útil, por exemplo, para fazeres SEO i18n. É apenas um dispatcher para redirecionar os pedidos HTTP para o controlador/view correto podes ver mais aqui: https://docs.djangoproject.com/en/2.2/topics/http/urls/. A primeira validação que tens de fazer é confirmar que o teu "File_b" está a executar corretamente. Podes usar return HttpResponse("batatas") para ver se está a executar corretamente. 10 REM Generation 48K! 20 INPUT "URL:", A$ 30 IF A$(1 TO 4) = "HTTP" THEN PRINT "400 Bad Request": GOTO 50 40 PRINT "404 Not Found" 50 PRINT "./M6 @ Portugal a Programar." Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now