简体   繁体   中英

Read from file output to html template in django

How would the solution linked below be written in views.py to read from multiple files and output those various fields? I can make this work for one but if I try copy pasting f = open('path/text.txt', 'r') file_content = f.read() f.close() context = {'file_content': file_content}

and changing the letters slightly to have unique versions it seems to break it...I am passing in unique variations not reusing "f" ex: h = open(BASE_DIR, 'path/tofile') file_content = h.read() h.close() context = {'file_contenth': file_contenth}

I then pass in to the return return render(request, pathtohtml, {'file_content': file_content}, {'file_contenth': file_contenth}

and that "breaks" it. I've tried a few variations of passing those variables in to no avail

I used the solution found here Django: Display contents of txt file on the website

正确的格式如下:

return render(request, path/to/html, {'file_content': file_content, 'file_contenth': file_contenth, 'other_variable': other_variable})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM