how to set filefield's url

i use new FormData() in js instead of form in html,so “upload_to” in FileField not works anymore.
and the file’s url is right when server in localhost,but not right in remote server.
in remote server the url looks like this:
it should be:
so,how to fix it?

here is upload code:

def upload_file(file):
    if file is not None:
        import os
        dir_path = settings.MEDIA_ROOT
        (filename, extension) = os.path.splitext(
        filename += '_' + uuid.uuid4().hex
        filename += extension
        if not os.path.exists(dir_path):
        file_path = os.path.join(dir_path, filename)
        with open(file_path, 'wb') as f:
            for chunk in file.chunks():
        return file_path

You should return the [media_url] (http s:// instead of the [media_root] (http s://

I am not sure what your use case is - but you could look at [File_Field] (http s:// which might provide the functionality that you might want to use. If its not quite what you want you can look at the [source] (http s:// of the field and get an Idea how it is managed internally.

A good explanation on the broad topic would be [Managing Files] (http s://

If all this didn’t help - please explain the thing that you are trying to accomplish a bit further, I might just didn’t catch it right.