title: Django Open and View a PDF(stored in AWS) in a new Tab
body: <p>I have a model that stores a PDF file. On the DetailView of that Model I would like to have a link to view the PDF in a new tab. There are similar questions on here that I've used to get this far, but it looks like they're for local PDFs. My PDFs are stored as Media on AWS.</p>
  
  <p>This is what I have right now: </p>
  
  <p>plans/models.py</p>
  
  <pre><code>class PlanSet(PlanModel):
      ...
  
  class PlanSetPDF(models.Model):
      planset = models.ForeignKey(PlanSet)
      PDF = models.FileField(upload_to='planPDFs')
      created = models.DateField(auto_now_add=True)
      added_by = models.ForeignKey(settings.AUTH_USER_MODEL, null=True)
  </code></pre>
  
  <p>plans/views.py</p>
  
  <pre><code>class PlanSetDetailView(DetailView):
  
      model = PlanSet
      template_name = "plan_detail.html"
  
      def get_context_data(self, **kwargs):
          context = super(PlanSetDetailView, self).get_context_data(**kwargs)
          context['now'] = timezone.now()
          return context
  
  def view_pdf(request, pk):
      pdf = get_object_or_404(PlanSetPDF, pk=pk)
      image_data = open(pdf.PDF.url, "rb").read()
      return HttpResponse(image_data, contenttype='application/pdf')
  </code></pre>
  
  <p>plans/urls.py</p>
  
  <pre><code>urlpatterns = [
      url(r'^(?P&lt;pk&gt;\d+)/$', plan_detail, name='detail'),
      url(r'^(?P&lt;pk&gt;\d+)/pdf/$', view_pdf, name='view_pdf'),
  ]
  </code></pre>
  
  <p>urls.py:     </p>
  
  <pre><code>url(r'^plans/', include(plan_sets.urls, namespace='plans')),
  </code></pre>
  
  <p>template:</p>
  
  <pre><code>{% for pdf in object.plansetpdf_set.all %}
    &lt;a href="{% url 'plans:view_pdf' pdf.pk %}"&gt;
    &lt;span class="glyphicon glyphicon-file" aria-hidden="true"&gt;&lt;/span&gt;PDF&lt;/a&gt;
  {% endfor %}
  </code></pre>
  
  <p>I'm recieving a builtin.OSError:[Errno 22] Invalid Argument: '<a href="https://link_to_PDF_file_on_amazon" rel="nofollow noreferrer">https://link_to_PDF_file_on_amazon</a>'</p>
  
  <p>It looks like Python can't open the file. I've looked around and I can't find any answers. Can one of you show me where I'm going wrong? Thanks.</p>