I'm trying to view a file I uploaded django FileField but it's not work

Hi friends I got stuck in problem while trying to view a pdf file in my page which I uploaded

have this models:

class HomeWork(models.Model):
  nameFile = models.CharField('Name File',max_length=30)
  file = models.FileField('File',upload_to="files",null=True)
  course = models.ForeignKey(Course, on_delete=models.CASCADE)
  user = models.ForeignKey(User,on_delete=models.CASCADE, null=True)

i make this field because i want to upload pdf files.

 file = models.FileField('File',upload_to="files",null=True)

in the settings.py i make this:

MEDIA_ROOT = BASE_DIR / 'static/upload'
MEDIA_URL = "/upload/"

here i have the html file that i want to see my upload file Whenever I try to access to: <iframe src="{{hw.file.url}}" frameborder="10"></iframe> it's not show me the pdf.

{% block content %}

<h1>{{course.name_course}}</h1>


{% if user.is_authenticated %}
  <a href="{% url 'Category:u-f' course.id  user.id%}">Upload FILE</a>
{% endif %}

{% if homeworks %}
<div class="row">
  {% for hw in homeworks %}
  <div class="col-md-4">
    <br>
    <div class="card">
      {{hw.file}}
      <iframe src="{{hw.file.url}}" frameborder="10"></iframe>
      <div class="card-body">
        <h4 class="card-title text-center"> <a class="Course" href=""><b>{{hw.nameFile}}</b></a></h4>
        <h5 class="card-title"></h5>
      </div>
      <div class="col text-center">   
      </div>
        <br>
    </div>
  </div>
  {% endfor %}
</div>
{% endif %}

{% endblock content %}

here some photo how its look like in html enter image description here

But whenever I make that line: <iframe src="{{hw.file.url}}" frameborder="10"></iframe> to static like that: <iframe src="{% static 'upload/files/avl_fib_proof.pdf' %}" frameborder="10"></iframe>

It looks like this:

enter image description here

Back to Top