Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6987867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:59:39+00:00 2026-05-27T18:59:39+00:00

I am trying to set up a system in which user uploaded files will

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T18:59:40+00:00Added an answer on May 27, 2026 at 6:59 pm

    Do you have enctype="multipart/form-data" set in your <form> tag? If request.FILES is empty, the problem isn’t with your dynamic path generation (it hasn’t gotten that far yet).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'll try my best to explain how I'm trying to set up this system.
I'm trying to set up a page that will allow users to download files
I am trying to read a text file which is set in CLASSPATH system
I'm trying to set up a messaging system where a user's messages are displayed
I am trying to archive a set of files from the source path to
I am trying to create a system which enables user to upload a zipfile,
I am trying to open a file which a user can set. In other
I'm trying to set up a system that displays calendar items from a resource
I am trying to set up an automated build system on Windows using Cygwin.
I am trying to set my columns default date time to system datetime. It

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.