I want to upload a file to my database and after it is uploaded import it and eventually export the data into my database. I have the uploading working just fine but I'm not sure how to get the absolute path of the file after it is uploaded. I'm able to print out the name of the document, but if the same document name is uploaded it is appended but still shows the original file name if I call form.cleaned_data['document'].name
. What can I do to get the absolute file path and then call a function to start processing this file?
So this is what I'm looking to do:
- User uploads a .csv file
- File gets saved in db (with a description and file path. File path is getting stored properly in db)
- Get file location of file that was just uploaded
- Start to process this file to convert the .csv data and store in database
models.py
from django.db import models
# Create your models here.
class Document(models.Model):
description = models.CharField(max_length=255, blank=True)
document = models.FileField(upload_to='documents/')
uploaded_at = models.DateTimeField(auto_now_add=True)
views.py
from django.shortcuts import render, redirect
from django.views import View
# Create your views here.
from .forms import DocumentForm
from .models import Document
class FileUpload(View):
def post(self, request):
form = DocumentForm(request.POST, request.FILES)
if form.is_valid():
print()
print(form.cleaned_data['document'].name)
form.save()
return redirect('main_db_model:home')
else:
return render(request, 'file_upload_form.html', {
'form': form
})
def get(self, request):
form = DocumentForm()
return render(request, 'file_upload_form.html', {
'form': form
})
forms.py
from django import forms
from .models import Document
class DocumentForm(forms.ModelForm):
class Meta:
model = Document
fields = ('description', 'document', )
file_upload_form.html (template):
{% extends "base.html" %}
{% block content %}
<form method="post" enctype="multipart/form-data">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Upload</button>
</form>
{% if saved %}
<strong>Your profile was saved.</strong>
{% endif %}
This is the form page
<p><a href="{% url 'main_db_model:home' %}">Return to home</a></p>
<p>Uploaded files:</p>
<ul>
{% for obj in documents %}
<li>
<a href="{{ obj.document.url }}">{{ obj.document.name }}</a>
<small>(Uploaded at: {{ obj.uploaded_at }})</small>
{{ obj.document.url }}
</li>
{% endfor %}
</ul>
{% endblock %}