how to create and show a temporary file

566 views
Skip to first unread message

Alex

unread,
Aug 11, 2008, 7:58:47 AM8/11/08
to Django users
Hi all.

I need to create a temporary html file and show it in an iframe in my
template.
So in the view I create a temporary file using python
NamedTemporaryFile, write content in it, and I pass it's name to the
template.

I used this configuration in urls.py
(r'^tmp/(?P<path>.*)$', 'django.views.static.serve', {'document_root':
'/tmp'}),

so the tempfile should be available at the following url:
http://localhost:8000/tmp/tempfilename

In my template a have a simple iframe like the following:
<iframe src="/tmp/tempfilename">
</iframe>

The problem is that the view does not seems to create the temp file,
i.e. the file is not created when I check the content of the system
temp directory (/tmp)

Someone have any idea about how to accomplish this task?

Thanks in advance.

Daniel Roseman

unread,
Aug 11, 2008, 8:26:48 AM8/11/08
to Django users
You haven't shown us the code of the view that's creating the file, so
we can't say what's wrong with it.

However I wonder why you need to create a file at all. Why not just
serve a normal view in the iframe, just as you do everywhere else?
There's nothing special about the content of an iframe from the point
of view of the server, so Django is just as capable as serving it up
dynamically as any other page.
--
DR.

Alex

unread,
Aug 11, 2008, 9:44:03 AM8/11/08
to Django users


On 11 Ago, 14:26, Daniel Roseman <roseman.dan...@googlemail.com>
wrote:

> You haven't shown us the code of the view that's creating the file, so
> we can't say what's wrong with it.

You're right. Here's the relevant code snippet in views.py:
#...
tempfile = NamedTemporaryFile()
tempfile.file.write(content)
response['tempfile'] = tempfile.name.split('/')[-1]
tempfile.file.close()
#....
variables = RequestContext(request, response)
return render_to_response('my_template.html', variables)

>
> However I wonder why you need to create a file at all. Why not just
> serve a normal view in the iframe, just as you do everywhere else?
> There's nothing special about the content of an iframe from the point
> of view of the server, so Django is just as capable as serving it up
> dynamically as any other page.

It seems a better approach. I'll try this and see if it fits my needs.

Thank you.

Alex



> --
> DR.
Reply all
Reply to author
Forward
0 new messages