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 7557941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T12:15:50+00:00 2026-05-30T12:15:50+00:00

How can I allow users to upload files to their own, user designated folder,

  • 0

How can I allow users to upload files to their own, user designated folder, and only see files that they have uploaded? I am using django file-transfer. Currently it gives me a choice of what file to put the media in, but I can put it in any user’s file and view every user’s media. Here is my uploads/models.py:

from django.db import models
from django.contrib.auth.models import User, UserManager

def uploadmodel_file_upload_to(instance, filename):
    print 'instance.user.username = '+ str(instance.user.username)
    return 'uploads/%s/%s' % (instance.user.username, filename)

class UploadModel(models.Model):
    user = models.ForeignKey('auth.user')
    file = models.FileField(upload_to=uploadmodel_file_upload_to)
  • 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-30T12:15:52+00:00Added an answer on May 30, 2026 at 12:15 pm

    uploadmodel_file_upload_to returns a relative path. To build the full path, django prepends settings.MEDIA_ROOT. MEDIA_ROOT is supposed to be public readable.

    So we want to save the file outside MEDIA_ROOT. Add something like this to settings.py:

    import os.path
    PROJECT_ROOT=os.path.abspath(os.path.dirname(__file__))
    PROTECTED_MEDIA_ROOT=os.path.join(PROJECT_ROOT, 'protected_uploads')
    

    Now you can update uploadmodel_file_upload_to to return an absolute path:

    def uploadmodel_file_upload_to(instance, filename):
        return '%s/%s/%s' % (settings.PROTECTED_MEDIA_ROOT, instance.user.username,
            filename)
    

    Now that the files are saved in /project/path/protected_uploads, we need to add a view to serve it, for example:

    import os 
    import mimetypes
    
    from django import shortcuts
    from django import http
    from django.conf import settings
    from django.views.static import was_modified_since
    from django.utils.http import http_date
    
    from .models import *
    
    def serve_upload(request, upload_id):
        upload = shortcuts.get_object_or_404(UploadModel, pk=upload_id)
        fullpath = upload.file.path
    
        if request.user != upload.user:
            return http.HttpResponseForbidden()
    
        statobj = os.stat(fullpath)
        mimetype, encoding = mimetypes.guess_type(fullpath)
        mimetype = mimetype or 'application/octet-stream'
        if not was_modified_since(request.META.get('HTTP_IF_MODIFIED_SINCE'),
                                  statobj.st_mtime, statobj.st_size):
            return http.HttpResponseNotModified(mimetype=mimetype)
        response = http.HttpResponse(open(fullpath, 'rb').read(), mimetype=mimetype)
        response["Last-Modified"] = http_date(statobj.st_mtime)
        response["Content-Length"] = statobj.st_size
        if encoding:
            response["Content-Encoding"] = encoding
        return response
    

    And a URL:

    url(r'serve_upload/(?P<upload_id>\d+)/$', 'serve_upload'),
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a web application that allow user upload their files on the app.
I have a form that allow users to upload files, I however need to
I am wanting to allow users to tag items so that they can search
I have an asp.net application where the users can upload files to our database.
I am writing a webpage that allows users to upload their files to Window
I have this codes to allow users to upload csv files and the contents
I want to allow user to enter only english alphabets and characters. How can
I have some code that allows users to upload file attachments into a varbinary(max)
How can i offer custom url's for files uploaded by users. For ex: user
I am trying to allow users to upload files for a django app running

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.