Q:

Django File upload - Path to file

I am uploading a file using the model.FileField method, but I am at a loss on how to actually return the path of the uploaded file (I can't recreate the path as there can be duplicate files and the imagename changes)
Views.py 
if form.is_valid():

        newdoc = Invoices(docfile = request.FILES['docfile'])
        newdoc.save()

Models.py
class Invoices(models.Model):
    docfile = models.FileField(upload_to='invoices/')    
    timestamp = models.DateTimeField(auto_now_add=True)
    username = models.CharField(max_length=20, default="default_user")

I found some references to document.uploaded_file path etc, but they throw an error - no property document. Is it possible using the method above to get the actual path to the saved file including the possibly changed filename?

A:

You can get the full filesystem path to your file accessing newdoc.docfile.path - in a template you'll definitely want to use the {{ newdoc.docfile.url }} attribute. For more information see documentation on FileField.

