Django upload image to cdn using an API
<p>I am building a website in Django on Pythonanywhere.com and I am using Backblaze's B2 cloud storage to store all the static and media files. My css and images that I have uploaded to Backblaze are working but I can't figure out how everything should fit together, so here is where I am (minimalized):</p>

<p>In this model I want to store a thumbnail image, I have a form working to upload it.</p>

<pre><code>class Post(models.Model):
    (...)
    thumbnail = models.ImageField(null=True, blank=True, width_field="width_field", height_field="height_field")
    (...)
</code></pre>

<p>Backblaze has all the code for the http requests and responses that I need, so I just pasted that in a seperate file. I first need to get an account authorization token, followed by an upload url and then I can send the file.</p>

<p>So that whole function needs three things as input: the file date, file name and file size. As output I get the file id (and other things, details <a href="https://www.backblaze.com/b2/docs/b2_upload_file.html" rel="nofollow">here</a>).</p>

<p>Now I wonder where I need to call that upload function, I assume it has to do with the "upload_to" parameter in the ImageField. And I wonder what actually gets stored in the ImageField, since I don't tell ImageField the location where to find the file. Does it use the media root in the settings file, how would I manage this?</p>

<p>Using these commands in settings, You can set MEDIA_URL as well as where image is to be stored. Here image is stored in src/media_cdn</p>

<pre><code>MEDIA_URL = "/media/"
MEDIA_ROOT = os.path.join(BASE_DIR, "media_cdn")
</code></pre>

<p>You have to tell the path where upload_to should save the image inside media_cdn else it will save image inside media_cdn, if upload_to is set to None.</p>

<pre><code>models.ImageField(upload_to='profiles/')
</code></pre>

<p>You need to upload image. So, that it contains address where image exist.</p>

python django file-upload cdn