I am trying to set up a system in which user uploaded files will be given a path based on username and filename. The answer to this question seems to have pointed me in the right direction, but I'm having a little trouble with the implementation from the view.
I have a model that uses this:
class Document(models.Model):
user = models.ForeignKey(User)
name = models.CharField(max_length=100, blank=False, null=False)
date = models.DateTimeField(auto_now=True)
path = models.FileField(upload_to= custom_doc_file_path) # path should be /media/users/USERNAME/DOCUMENT_NAME
and custom_doc_file_path is defined like so:
def custom_doc_file_path(instance, filename):
# creates media/users/USERNAME/FILENAME
return '/'.join(['users', instance.user.username, get_directory_name(filename), filename])
def get_directory_name(filename):
directory = filename.replace('.', '_')
directory = directory.replace(' ', '_')
return directory
This works great when uploading a file from the admin view, but I can't seem to replicate it from a view I'm creating. My view looks like this:
def create_button(request):
if request.method == 'POST':
document = Document.objects.create(
user = request.user,
name = request.POST['name'],
path = request.POST['uploaded_file']
)
document.save()
But when I try this the path is saved as the name of the file. If I try leaving the path blank and then saving the file to that path the model doesn't create a path at all. Additionally I would think that the file should be in request.FILES
but this is shown as an empty dict.
Essentially I just want to know what I need to do to replicate the behavior of the admin when I add a new document through this model.