title: How to open an uploaded file in another template - Django?
body: <p>my django app acts as an emailing service, emails that are sent are view-able and it's possible to send html emails. How do I display the html emails on the view-mail page rather than just displaying the name of the file ? (the following is the mail-view for an html email): <a href="https://i.stack.imgur.com/Pzdt4.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Pzdt4.png" alt="enter image description here"></a></p>
  
  <p>How would I display the actual html in the body of this page?</p>
  
  <p>This is my views.py:</p>
  
  <pre><code>def outbox(request):
  #Mail_Item.objects.all().delete()
  
  if request.method == "POST" and request.POST.get('Username', False)!=False:
      username = request.POST.get('Username')
      password = request.POST.get('Password')
      user = authenticate(request, username=username, password=password)
      if user is not None:
          login(request, user, backend='django.contrib.auth.backends.ModelBackend')
          print(username + " has logged in")
  
          messages = Mail_Item.objects.filter(user=request.user.username)
          args = {'messages':messages}
  
          return render(request, 'outbox.html', args)
      else: 
          return render(request, '404.html')
  
  elif request.POST.get('Username', False) == False and request.POST.get('mail_body_field', False) == False:
      pass
  
  elif request.method == "POST" and request.POST.get('mail_subject_field')!=False:
      subject = request.POST.get('mail_subject_field')
      body = request.POST['mail_body_field']
      file = ""
      try:
          file = request.FILES['filename']
  
      except:
          pass
  
      print("sending mail:")
  
      for i in range(1, len(Res)+1):
          y = Res['ID' + str(i)].email
          print("sent to " + y )
          msg = EmailMessage(subject, body, 'email@example.com', [y])
          msg.content_subtype = "html"
          if (str(file)) != "" and (str(file))[-4:] != 'html':
  
              msg.attach_file(str(file))
              obj = Mail_Item(subject=subject, body=body, user=request.user.username, file_name=(str(file)))
              obj.save()
              print("email stored in database")
  
          elif (str(file)) != "" and (str(file))[-4:] == 'html' :
  
              uploaded_file = file
              fs = FileSystemStorage()
              fs.save((str(file)), uploaded_file)
  
              html_message = render_to_string((str(file)), {'context': 'values'})
              plain_message = strip_tags(html_message)
              from_email = 'From &lt;email2@example.com&gt;'
              mail.send_mail(subject, plain_message, from_email, [y], html_message=html_message)
              obj = Mail_Item(subject=subject, body=body, user=request.user.username, file_name=(str(file)))
              obj.save()
  
          else:
  
              obj = Mail_Item(subject=subject, body=body, user=request.user.username, file_name='None')
              obj.save()
              print("email stored in database")
          #msg.send()
          i += 1
  
      messages = Mail_Item.objects.filter(user=request.user.username)
      args = {'messages':messages}
  
      return HttpResponseRedirect('../templates/outbox.html', args, request)
  else:
      print("pleeeeeaassee")
      return render(request, '404.html')
  
  messages = Mail_Item.objects.filter(user=request.user.username)
  args = {'messages':messages}  
  
  
  return render(request, 'outbox.html', args)
  
  def login_page(request):
  
  if request.method == "POST":
      username = request.POST['sign-up-name']
      email = request.POST['sign-up-email']
      password = request.POST['sign-up-password']
      user = User.objects.create_user(username, email, password)
      user.save()
      print(username + " has been added to the user database.")
  else:
      pass
  return render(request, 'login.html')
  
  def signup_page(request):
      return render(request, 'signup.html')
  
  def mail_view(request, id=None):
      if id:
          email = Mail_Item.objects.get(id=id)
          args = {'email':email}
      else:
          pass
      return render(request, 'mail-view.html', args)
  
  def log_out(request):
      logout(request)
      return render(request, 'log-out.html')
  
  def delete(request):
      Mail_Item.objects.filter(id=id).delete()
      messages = Mail_Item.objects.filter(user=request.user.username)
      args = {'messages':messages}  
  
      return render(request, 'outbox.html', args)
  </code></pre>
  
  <p>This is the code for my mail-view template where I'd like to place the html that is being sent: </p>
  
  <pre><code>&lt;div class="mail-view"&gt;
                      &lt;h4 class="m-0"&gt;{{ email.subject }}&lt;/h4&gt;
  
                      &lt;div class="divid"&gt;&lt;/div&gt;
                      &lt;div class="media"&gt;
                          &lt;div class="media-left"&gt;
                              &lt;div class="avatar avatar-lg avatar-circle"&gt;
                                  &lt;img class="img-responsive" src="{% static '../static/assets/images/221.jpg' %}" alt="avatar"/&gt;
                              &lt;/div&gt;&lt;!-- .avatar --&gt;
                          &lt;/div&gt;
  
                          &lt;div class="media-body"&gt;
                              &lt;div class="m-b-sm"&gt;
                                  &lt;h4 class="m-0 inline-block m-r-lg"&gt;
                                      &lt;a href="#" class="title-color"&gt;Access Bank&lt;/a&gt;
                                  &lt;/h4&gt;
                              &lt;/div&gt;
                              &lt;p&gt;&lt;b&gt;Attachment: &lt;/b&gt;{{ email.file_name }}&lt;/p&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
                      &lt;div class="divid"&gt;&lt;/div&gt;
  
                      &lt;div class="row"&gt;
                          &lt;div class="col-md-12"&gt;
                              &lt;div class="m-h-lg lh-xl"&gt;
                                  &lt;p&gt;{{ email.body }}&lt;/p&gt;
                              &lt;/div&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
  </code></pre>
  
  <p>Models.py: </p>
  
  <pre><code> from django.db import models
  
  
  class Details(models.Model):
      email = models.CharField(max_length=200)
      def __str__(self):
          return self.email
  
  class Mail_Item(models.Model):
      subject = models.CharField(max_length=200)
      body = models.CharField(max_length=300)
      time = models.DateTimeField(auto_now=True)
      user = models.CharField(max_length=15)
      file_name = models.CharField(max_length=100, null=True, default=None)
  
  
      def __str__(self):
          template = '{0.subject} {0.body} {0.time} {0.user} {0.file_name}'
          return template.format(self)
  </code></pre>