render .txt file in django template

173 views
Skip to first unread message

Fatih Tiryakioglu

unread,
Jan 30, 2014, 4:29:29 PM1/30/14
to django...@googlegroups.com
Hi all,

Could you please show me how can I render .txt static files (they are user comments) in django template? I couldn't resolve that issue for a month..

I appreciate your help.


--

C. Kirby

unread,
Jan 30, 2014, 4:33:11 PM1/30/14
to django...@googlegroups.com
The template engine doesn't have direct access to the filesystem. You will have to pass the file contents from your view to the template:

1.Load the file in your view
2. Read it into a context variable.
3. Render the context variable in your template

Fatih Tiryakioglu

unread,
Jan 30, 2014, 4:57:41 PM1/30/14
to django...@googlegroups.com
But the document says that static media isn't about django, but apache (for production). Loading static file contents in the view.py seems to me not effecient.

I am now on development phase, and I think I should render static files in the template now. And in the production phase, I would change just the shortcuts..

Am I wrong?


--


30 Ocak 2014 Perşembe 23:33:11 UTC+2 tarihinde C. Kirby yazdı:

C. Kirby

unread,
Jan 30, 2014, 5:09:55 PM1/30/14
to django...@googlegroups.com
Sort of wrong. Django static files are available as http resources, not available to the template renderer. With django staticfiles you can't do {{file_i_want}}.

If you want to work purely in the template, you have 2 options:
1. Load the txt file url with ajax and put it where you want on the page
2. {% ssi %} if you have the absolute path to the txt file
Reply all
Reply to author
Forward
0 new messages