<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[mjoghenemaega's]]></title><description><![CDATA[mjoghenemaega's]]></description><link>https://mjoghenemaega.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Fri, 11 Sep 2026 12:46:38 GMT</lastBuildDate><atom:link href="https://mjoghenemaega.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Django ImageField - Easy steps to use it in  your model]]></title><description><![CDATA[What exactly is a Django ImageField?
The imageField in a Django model is a part of a FileField that adds image storage functionality to your model. Though using this field on your model is a bit easy, you have to be careful most of the time because i...]]></description><link>https://mjoghenemaega.hashnode.dev/django-imagefield-easy-steps-to-use-it-in-your-model</link><guid isPermaLink="true">https://mjoghenemaega.hashnode.dev/django-imagefield-easy-steps-to-use-it-in-your-model</guid><category><![CDATA[Python]]></category><category><![CDATA[Django]]></category><category><![CDATA[Web Development]]></category><category><![CDATA[backend]]></category><dc:creator><![CDATA[moses johnson oghenemaega]]></dc:creator><pubDate>Tue, 28 Sep 2021 13:44:35 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1632812398524/SOTz7JOh8.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h3 id="what-exactly-is-a-django-imagefield">What exactly is a Django ImageField?</h3>
<p>The imageField in a Django model is a part of a FileField that adds image storage functionality to your model. Though using this field on your model is a bit easy, you have to be careful most of the time because it can get complicated very fast. This post I have written will take you through steps on how you can use the image field efficiently on your model.</p>
<h3 id="using-the-imagefield">Using the imageField</h3>
<p><strong>installing pillow</strong></p>
<p>First, before adding the image field to your model, you need to install the  <a target="_blank" href="https://pillow.readthedocs.io/en/stable/">pillow library</a> (which will help you render the images smoothly).</p>
<p>To install run</p>
<p><code>pip install pillow</code> </p>
<p>This library provides extensive file format support, an efficient internal representation, and powerful image processing capabilities.</p>
<p><strong>Adding the imageField</strong></p>
<p>Now you can add an image field to your model.</p>
<pre><code><span class="hljs-keyword">from</span> django.core.files <span class="hljs-keyword">import</span> File
<span class="hljs-keyword">from</span> django.db <span class="hljs-keyword">import</span> models
<span class="hljs-keyword">from</span> PIL <span class="hljs-keyword">import</span> Image
<span class="hljs-keyword">from</span> django.contrib.auth.models <span class="hljs-keyword">import</span> User


<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Product</span>(<span class="hljs-params">models.Model</span>):</span>
    title = models.CharField(max_length=<span class="hljs-number">255</span>)
    slug = models.SlugField(max_length=<span class="hljs-number">255</span>)
    description = models.TextField(blank=<span class="hljs-literal">True</span>, null=<span class="hljs-literal">True</span>)
    price = models.FloatField()
                <span class="hljs-comment">#adding the image field</span>
    image = models.ImageField(upload_to=<span class="hljs-string">'uploads/'</span>, blank=<span class="hljs-literal">True</span>, null=<span class="hljs-literal">True</span>)
    thumbnail = models.ImageField(upload_to=<span class="hljs-string">'uploads/'</span>, blank=<span class="hljs-literal">True</span>, null=<span class="hljs-literal">True</span>)
    date_added = models.DateTimeField(auto_now_add=<span class="hljs-literal">True</span>)
</code></pre><p>Next, configure the file path to store the image uploaded using the MEDIA_ROOT and MEDIA_URL. Copy the code below into your <em> settings</em> file.</p>
<pre><code><span class="hljs-attr">MEDIA_ROOT</span> = os.path.join(BASE_DIR, <span class="hljs-string">'media/'</span>)
<span class="hljs-attr">MEDIA_URL</span> = <span class="hljs-string">'/media/'</span>
</code></pre><p><em>The media_url is the URL that points to the directory the media file uploaded is stored, while the media_root is the path that stores the media files.</em></p>
<p>With this, You are almost set!</p>
<p>Now, update your urls.py file as shown below:</p>
<pre><code><span class="hljs-keyword">from</span> django.urls <span class="hljs-keyword">import</span> <span class="hljs-type">path</span>
<span class="hljs-keyword">from</span> django.conf <span class="hljs-keyword">import</span> settings
<span class="hljs-keyword">from</span> django.conf.urls.static <span class="hljs-keyword">import</span> static

urlpatterns = [
    path(<span class="hljs-string">'admin/'</span>, <span class="hljs-keyword">admin</span>.site.urls),

] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
</code></pre><p>After adding this, your Django development server will be able to serve media files both in development and production mode. </p>
<p><strong>Updating the server </strong></p>
<p>We can now update the server, update the development server by running the <strong>migrations.</strong></p>
<pre><code><span class="hljs-selector-tag">python</span> <span class="hljs-selector-tag">manage</span><span class="hljs-selector-class">.py</span> <span class="hljs-selector-tag">makemigrations</span>
<span class="hljs-selector-tag">python</span> <span class="hljs-selector-tag">manage</span><span class="hljs-selector-class">.py</span> <span class="hljs-selector-tag">migrate</span>
</code></pre><p><strong>Note:</strong> <em>to avoid errors such as the one below, make sure you have the pillow library installed before running migrations</em></p>
<pre><code>magazine.Magazine.image: (fields.E210) Cannot <span class="hljs-keyword">use</span> ImageField because Pillow <span class="hljs-keyword">is</span> <span class="hljs-keyword">not</span> installed.
        HINT: <span class="hljs-keyword">Get</span> Pillow <span class="hljs-keyword">at</span> https://pypi.org/<span class="hljs-keyword">project</span>/Pillow/ <span class="hljs-keyword">or</span> run command <span class="hljs-string">"python -m pip install Pillow"</span>.
post.Posts.image: (fields.E210) Cannot <span class="hljs-keyword">use</span> ImageField because Pillow <span class="hljs-keyword">is</span> <span class="hljs-keyword">not</span> installed.
        HINT: <span class="hljs-keyword">Get</span> Pillow <span class="hljs-keyword">at</span> https://pypi.org/<span class="hljs-keyword">project</span>/Pillow/ <span class="hljs-keyword">or</span> run command <span class="hljs-string">"python -m pip install Pillow"</span>.
post.Sample.image: (fields.E210) Cannot <span class="hljs-keyword">use</span> ImageField because Pillow <span class="hljs-keyword">is</span> <span class="hljs-keyword">not</span> installed.
        HINT: <span class="hljs-keyword">Get</span> Pillow <span class="hljs-keyword">at</span> https://pypi.org/<span class="hljs-keyword">project</span>/Pillow/ <span class="hljs-keyword">or</span> run command <span class="hljs-string">"python -m pip install Pillow"</span>.
post.Workers.avatar: (fields.E210) Cannot <span class="hljs-keyword">use</span> ImageField because Pillow <span class="hljs-keyword">is</span> <span class="hljs-keyword">not</span> installed.
        HINT: <span class="hljs-keyword">Get</span> Pillow <span class="hljs-keyword">at</span> https://pypi.org/<span class="hljs-keyword">project</span>/Pillow/ <span class="hljs-keyword">or</span> run command <span class="hljs-string">"python -m pip install Pillow"</span>.
</code></pre><p>Once done with the steps above, run the server.</p>
<pre><code><span class="hljs-selector-tag">python</span> <span class="hljs-selector-tag">manage</span><span class="hljs-selector-class">.py</span> <span class="hljs-selector-tag">runserver</span>
</code></pre><p>The above function will run the development server. Login to the admin environment the see the image field.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1632833215666/1D_ZDZpF5.jpeg" alt="adin_login _page (2).jpg" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1632831409023/1W6dgv6r_M.jpeg" alt="admin_model_page.jpg" /></p>
<p>The user can now upload the image from your local storage by clicking the choose file button.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1632833238864/UtkMhnm43.png" alt="Annotation 2021-09-28 133824.png" /></p>
]]></content:encoded></item></channel></rss>