1

I have a model called Advertisement. This model has text field, title field, and file field.

I am successfully saving all 3 of these fields into the model.

Now I need to show them in template. My vision is:

ads = Advertisement.objects.all()
return render_to_response('page.html', {'ads':ads},context_instance=RequestContext(request))

and in my page.html:

{% for each_ad in ads %}
<p>{{each_ad.title}}</p>
<p>{{each_ad.text}}</p>
<p><a href="/ads/{{each_ad.file_pdf}}>{{each_ad.file_pdf.name}}</a></p>
{% endfor %}

Does this seem right? If not, please show me the way so I can learn. Thanks!

Sam Mussmann
  • 5,883
  • 2
  • 29
  • 43
doniyor
  • 36,596
  • 57
  • 175
  • 260

1 Answers1

3

Whether the PDF link downloads depends on the browser of the user.

Adding target="_blank" will open the pdf in a new window/tab though:

<p><a href="/ads/{{each_ad.file_pdf}}" target="_blank">{{each_ad.file_pdf.name}}</a></p>

To ensure that it always downloads in every environment is more difficult, you would need to set your .htaccess (if you're using apache, per something like this: Force a file or image to download using .htaccess) or force it as an attachment in producing the PDF from Django.

Community
  • 1
  • 1
Williams
  • 4,044
  • 1
  • 37
  • 53
  • i am not trying to render the whole html page as pdf! this is not what i want. i just want to render pdf file to html page so that user can click and download the pdf file. nothing more – doniyor Nov 07 '12 at 16:18
  • okay thanks, opening or downloading the pdf is almost same, so opening is also fine. so my vision-code is okay? – doniyor Nov 07 '12 at 16:27