'How To Open and Read a File of Type FileField in Django?
I am trying to open a file that would be uploaded through the django FileField attribute. I also want to read the contents of the file and output it. But when I do open(obj.file), it gives me an error that read "expected str, bytes or os.PathLike object, not FieldFile" So, I am not sure how to get around this! I also tried using obj.file.read(),this does the job, but it gives these "\n" "\r" characters in the output.
This is my views.py
from django.shortcuts import render
from .forms import ReaderForm
def HomePage(request):
text = None
if request.method == "POST":
form = ReaderForm(request.POST or None, request.FILES or None)
if form.is_valid():
file = form.cleaned_data.get('file')
obj = form.save(commit=False)
obj.file = file
obj.save()
f = open(obj.file, "r")
print(f.read())
else:
form = ReaderForm()
context = {
"form": form,
"text": text
}
return render(request, "reader/home.html", context)
This is models.py
from django.db import models
from django.utils import timezone
class Reader(models.Model):
file = models.FileField(blank=True, null=True)
date_uploaded = models.DateTimeField(default=timezone.now)
class Meta:
ordering = ['-date_uploaded']
verbose_name_plural = "Reader"
This is my template file (home.html)
{% extends 'reader/base.html' %}
{% block head %}
<title>File Reader</title>
{% endblock %}
{% block content %}
<h1>File Reader</h1>
<form method="POST" enctype="multipart/form-data"> {% csrf_token %}
{{ form.as_p }}
<button type="submit">Submit</button>
</form>
{% if not text == None %}
<p>{{ text }}</p>
{% endif %}
{% endblock %}
And lastly, forms.py
from .models import Reader
from django import forms
class ReaderForm(forms.ModelForm):
class Meta:
model = Reader
fields = ['file']
Thank you for any answers!
Solution 1:[1]
The FieldFile
object includes its own open. Instead of:
f = open(obj.file,'r')
Do this:
f = obj.file.open('r')
Solution 2:[2]
FileField
(Django docs) has a path
property.
So you can do:
f = open(obj.file.path, "r")
print(f.read())
Or, as suggested in the comments, using the context manager:
with open(obj.file.path, "r") as f:
print(f.read())
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | phoenix |
Solution 2 |