# How is the file from OneToOne Model's FileField accessed, read in views.py and passed to template?

**URL:** <https://forum.djangoproject.com/t/how-is-the-file-from-onetoone-models-filefield-accessed-read-in-views-py-and-passed-to-template/13004>\
**Category:** Forms & APIs\
**Created:** [April 5, 2022, 2:36am UTC](https://forum.djangoproject.com/t/how-is-the-file-from-onetoone-models-filefield-accessed-read-in-views-py-and-passed-to-template/13004 "2022-04-05T02:36:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![khaw235](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/khaw235/32/6372_2.png) [@khaw235](https://forum.djangoproject.com/u/khaw235)\
**Post date:** [April 5, 2022, 2:36am UTC](https://forum.djangoproject.com/t/how-is-the-file-from-onetoone-models-filefield-accessed-read-in-views-py-and-passed-to-template/13004/1 "2022-04-05T02:36:51Z")

</div>

I am developing a simple Data Visualization App, where a user can register, login upload a file and visualize its content.

I am using default `User` model, and a `Detail` Model having OneToOne relation with `User` Model. The `Detail` has 3 fields, which are:

```auto
OneToOne(User)
FileField()
TextField()

```

Now, I want to access the file that is saved in `FileField, in `views.py`and read it's content, then visualize it using Python's`NumPy`and`Matplotlib`, then finally show visualization results on the frontend.

I need a guidance that how should I start and approach this task? Basically, I need deep guidance in how to access and read the file in `views.py`?

My **`models.py`** is:

```auto
class Detail(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    file = models.FileField(verbose_name="CSV File", upload_to='csv_files/')
    file_desc = models.TextField("CSV File Description")

    def __str__ (self):
        return ("{} ({} {})".format(self.user.email, self.user.first_name, self.user.last_name))

```

and in **`views.py`** , I am approaching it this way:

```auto
class VisualizeAPI(View):
    template_name = 'accounts/visualize.html'

    def get(self, request):
        msg = {'message': "Please login to view this page."}
        
        if request.user.is_authenticated:
            detail, _ = Detail.objects.get_or_create(user=request.user)
            context = {'detail': detail}

            return render(request, self.template_name, context)

        return render(request, self.template_name, msg)

```

and in `template`, I am approaching it this way:

```auto
<body>
    <h1>Visualized Details</h1>
 
    {% if request.user.is_authenticated %}
    {{ detail }}
    {% else %}
    <h2>{{ message }}</h2>
    {% endif %}
</body>

```

But it is not printing the content of the file on the frontend.  
I will be glad for proper approach and guidance provided.

---

<div class="post-metadata">

**Author:** ![khaw235](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/khaw235/32/6372_2.png) [@khaw235](https://forum.djangoproject.com/u/khaw235)\
**Post date:** [April 5, 2022, 1:14pm UTC](https://forum.djangoproject.com/t/how-is-the-file-from-onetoone-models-filefield-accessed-read-in-views-py-and-passed-to-template/13004/2 "2022-04-05T13:14:09Z")

</div>

@KenWhitesell any word of guidance on it?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [April 5, 2022, 1:58pm UTC](https://forum.djangoproject.com/t/how-is-the-file-from-onetoone-models-filefield-accessed-read-in-views-py-and-passed-to-template/13004/3 "2022-04-05T13:58:25Z")

</div>

See the docs at [Model field reference | Django documentation | Django](https://docs.djangoproject.com/en/4.0/ref/models/fields/#filefield-and-fieldfile).

The FileField in the model is not the contents of the file. It’s a reference to a file in your storage system.

Handling a file in a view is pretty close to being the same as handling a file anywhere else in Python. You need to read the contents of the file to do anything with those contents.

---

<div class="post-metadata">

**Author:** ![khaw235](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/khaw235/32/6372_2.png) [@khaw235](https://forum.djangoproject.com/u/khaw235)\
**Post date:** [April 5, 2022, 7:59pm UTC](https://forum.djangoproject.com/t/how-is-the-file-from-onetoone-models-filefield-accessed-read-in-views-py-and-passed-to-template/13004/4 "2022-04-05T19:59:49Z")

</div>

Hi @KenWhitesell thanks for the suggestion. Following the link you provided and some research, I was able to successfully load and read data of the file saved in `FileField` and then pass that to template.

Below is my `views.py`:

```auto
class VisualizeAPI(View):
    template_name = 'accounts/visualize.html'

    def get(self, request):
        msg = {'message': "Please login to view this page."}
        
        if request.user.is_authenticated:
            detail, _ = Detail.objects.get_or_create(user=request.user)
            if detail.file:
                file = detail.file.read().decode("utf-8")
                csv_data = csv.reader(StringIO(file), delimiter=",")
            else:
                csv_data = None
                
            context = {
                "detail": detail,
                "csv_data": csv_data,
            }

            return render(request, self.template_name, context)

        return render(request, self.template_name, msg)

```

and in template, I did the following:

```auto
<body>
    <h1>Visualized Details</h1>
    {% if request.user.is_authenticated %}
        {% for row in csv_data %}
            {{ row }}
        {% endfor %}
    {% else %}
    <h2>{{ message }}</h2>
    {% endif %}
</body>

```

Again thanks @KenWhitesell , I have posted another thread related to logging out user’s issue, please have a look there an guide me there as well. Thanks.
