<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Manasa SK</title>
    <description>The latest articles on DEV Community by Manasa SK (@manasask).</description>
    <link>https://dev.to/manasask</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F857250%2F65be16c8-6386-472d-a03b-88a3347e3f02.png</url>
      <title>DEV Community: Manasa SK</title>
      <link>https://dev.to/manasask</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/manasask"/>
    <language>en</language>
    <item>
      <title>Django Images: Static vs Media</title>
      <dc:creator>Manasa SK</dc:creator>
      <pubDate>Sun, 14 Aug 2022 06:51:00 +0000</pubDate>
      <link>https://dev.to/manasask/django-images-static-vs-media-21lm</link>
      <guid>https://dev.to/manasask/django-images-static-vs-media-21lm</guid>
      <description>&lt;p&gt;There are mainly 2 ways of displaying images on your website when using Django. Which method should you use then? It depends on the requirements and I will be providing a detailed understanding of how and when to use each method.&lt;/p&gt;

&lt;p&gt;The 2 ways of displaying images on a web page are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Static images&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Upload through media&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Static Images
&lt;/h2&gt;

&lt;p&gt;If an image is to be displayed from a definite set of images, static method should be used.&lt;/p&gt;

&lt;p&gt;Your folder structure inside the project folder must look something like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ª   db.sqlite3
ª   manage.py
ª   tree.txt
ª   
+---demo
ª   ª   asgi.py
ª   ª   settings.py
ª   ª   urls.py
ª   ª   wsgi.py
ª   ª   __init__.py
ª   ª   
ª   +---__pycache__
ª           settings.cpython-310.pyc
ª           urls.cpython-310.pyc
ª           wsgi.cpython-310.pyc
ª           __init__.cpython-310.pyc
ª           
+---env
ª
+---myapp
    ª   admin.py
    ª   apps.py
    ª   models.py
    ª   tests.py
    ª   views.py
    ª   __init__.py
    ª   
    +---migrations
            __init__.py
    ª
    +---templates
    ª       index.html
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create a folder in myapp and name it 'static'.&lt;/p&gt;

&lt;p&gt;myapp folder structure should look like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;+---myapp
    ª   admin.py
    ª   apps.py
    ª   models.py
    ª   tests.py
    ª   views.py
    ª   __init__.py
    ª   
    +---migrations
    ª       __init__.py
    ª
    +---static
    ª
    +---templates
    ª       index.html
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, in static folder, you can create a folder 'images' and upload your images.&lt;/p&gt;

&lt;p&gt;In your specified html file (index.html in this case), add the following at the top of the page:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;{% load static %}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;In the image tag of the file, add the path to the image by adding the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{% load static %}
&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html lang="en"&amp;gt;
&amp;lt;head&amp;gt;
    &amp;lt;meta charset="UTF-8"&amp;gt;
    &amp;lt;meta http-equiv="X-UA-Compatible" content="IE=edge"&amp;gt;
    &amp;lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&amp;gt;
    &amp;lt;title&amp;gt;Show Static Image&amp;lt;/title&amp;gt;
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;
    &amp;lt;img src="{% static 'images/image.jpg' %}" alt=""&amp;gt;
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since static url is already defined in settings.py, the static tag refers to the static folder created.&lt;/p&gt;

&lt;p&gt;You can display the image like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--WrJ-3AcT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l08pv0w3x6sine0wf4nq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--WrJ-3AcT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l08pv0w3x6sine0wf4nq.png" alt="Static image displayed on web page" width="880" height="424"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Upload Image through Media
&lt;/h2&gt;

&lt;p&gt;When multiple images need to be stored in the database or object or user specific images need to be displayed, this method is used.&lt;/p&gt;

&lt;p&gt;The images can be uploaded to the database through:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;A seperate form on a web page that takes image files&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Uploading images through admin panel&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here, I will be uploading the images through the admin panel.&lt;/p&gt;

&lt;p&gt;Since this method requires Pillow (python module), run the command:&lt;br&gt;
&lt;code&gt;pip install Pillow&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Create your model and create an image field.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from django.db import models

# Create your models here.
class MyModel(models.Model):
    id = models.AutoField(primary_key=True, auto_created=True)
    name = models.CharField(max_length=50)
    image = models.ImageField(upload_to='images/')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This automatically creates a folder 'media' and a folder 'images' in it once the setup is completed. Migrate the changes.&lt;/p&gt;

&lt;p&gt;In urls.py, mention the root to media:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from . import views
from django.urls import path
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    path('', views.index, name='index'),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Lastly, in settings.py, insert:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import os

MEDIA_ROOT =  os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now that the setup is completed, you can insert the data.&lt;/p&gt;

&lt;p&gt;To access the admin panel, create a superuser by running the command:&lt;br&gt;
&lt;code&gt;python manage.py createsuperuser&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now register your model in the admin panel by adding the following to admin.py:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from .models import *

admin.site.register(MyModel)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now switch to the admin panel and add a few objects to the model. Next, edit your view to pass image query set to the web page.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Create your views here.
def index(request):
    images = MyModel.objects.filter(name='manasa')
    return render(request, 'index.html', {'images': images})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And edit your html page to display these images.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html lang="en"&amp;gt;
&amp;lt;head&amp;gt;
    &amp;lt;meta charset="UTF-8"&amp;gt;
    &amp;lt;meta http-equiv="X-UA-Compatible" content="IE=edge"&amp;gt;
    &amp;lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&amp;gt;
    &amp;lt;title&amp;gt;Show Media Image&amp;lt;/title&amp;gt;
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;
    {% for img in images %}
    &amp;lt;img src="{{img.image.url}}" alt="" width="200px" height="150px"&amp;gt;
    {% endfor %}
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now that the procedure is completed, run the server. The web page should look like this:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--89OcAq02--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kfdlqcy780tjz8fzbyhu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--89OcAq02--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kfdlqcy780tjz8fzbyhu.png" alt="Images displayed through media" width="880" height="425"&gt;&lt;/a&gt;&lt;/p&gt;




</description>
      <category>python</category>
      <category>django</category>
      <category>webdev</category>
      <category>image</category>
    </item>
    <item>
      <title>Setup and Create your First Django Application</title>
      <dc:creator>Manasa SK</dc:creator>
      <pubDate>Sun, 07 Aug 2022 10:57:00 +0000</pubDate>
      <link>https://dev.to/manasask/setup-and-create-your-first-django-application-4eb3</link>
      <guid>https://dev.to/manasask/setup-and-create-your-first-django-application-4eb3</guid>
      <description>&lt;p&gt;Django is a python based high-level web framework which is primarily known for rapid development. It supports the Model View Controller (MVC) pattern.&lt;/p&gt;

&lt;p&gt;You can observe multiple websites that are built on this framework such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.instagram.com/"&gt;Instagram&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://in.pinterest.com/"&gt;Pinterest&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.openstack.org/"&gt;Open Stack&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you're new to web development, Django is a fairly easy framework to understand and work on. This was my first web framework and I would suggest it if:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Your project involves Machine Learning models or is of Scientific nature&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You have some experience or knowledge of Python&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Pre-requisites
&lt;/h2&gt;

&lt;p&gt;While Django framework is much simpler to setup relative to other technologies, some fundamental pre requisites include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Installation of Python&lt;/strong&gt; If python is not installed, &lt;a href="https://www.python.org/downloads/"&gt;click here&lt;/a&gt; and install one of the newer versions of python.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Basics of Python&lt;/strong&gt; Concept of OOPs, loops and conditional statements, data types and variables, functions and basic data structures are sufficient to get started.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Setup Django
&lt;/h2&gt;

&lt;p&gt;Create a new folder at your desired location and open the same in the terminal&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;PS C:\Users\Manasa\Desktop\first_project&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create a &lt;strong&gt;virtual environment&lt;/strong&gt; to install Django&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;PS C:\Users\Manasa\Desktop\first_project&amp;gt; python -m venv env
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, &lt;strong&gt;env&lt;/strong&gt; is the name of your virtual environment.&lt;/p&gt;

&lt;p&gt;A virtual environment is created for you to install a custom version of Python and its different packages which is not connected to the global installation on the server.&lt;/p&gt;

&lt;p&gt;Once the environment is created, activate it by running the command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;PS C:\Users\Manasa\Desktop\first_project&amp;gt; env/scripts/activate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Activated environment should look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(env) PS C:\Users\Manasa\Desktop\first_project&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, install Django&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; pip install django
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can check the installation by calling django-admin&lt;br&gt;
It should give this output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(env) PS C:\Users\Manasa\Desktop\first_project&amp;gt; django-admin

Type 'django-admin help &amp;lt;subcommand&amp;gt;' for help on a specific subcommand.

Available subcommands:

[django]
    check
    compilemessages
    createcachetable
    dbshell
    diffsettings
    dumpdata
    flush
    inspectdb
    loaddata
    makemessages
    makemigrations
    migrate
    optimizemigration
    runserver
    sendtestemail
    shell
    showmigrations
    sqlflush
    sqlmigrate
    sqlsequencereset
    squashmigrations
    startapp
    startproject
    test
    testserver
Note that only Django core commands are listed as settings are not properly configured (error: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.).
(env) PS C:\Users\Manasa\Desktop\first_project&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Create Project
&lt;/h2&gt;

&lt;p&gt;Now that Django is successfully installed, you can create your project and applications.&lt;/p&gt;

&lt;p&gt;Run the following command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; django-admin startproject first_project .
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, your project is named 'first_project'.&lt;/p&gt;

&lt;p&gt;NOTE: '.' at the end of the command is essential as the project structure by default makes the file manage.py inaccessible to a number of files. This can cause numerous errors going ahead.&lt;/p&gt;

&lt;p&gt;Now, create your app by running the command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; django-admin startapp myapp
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, your application is named 'myapp'.&lt;/p&gt;

&lt;p&gt;Now that your project and application are created, use manage.py to make further changes.&lt;/p&gt;

&lt;p&gt;Its functionality can be observed by the following command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(env) PS C:\Users\Manasa\Desktop\first_project&amp;gt; python manage.py

Type 'manage.py help &amp;lt;subcommand&amp;gt;' for help on a specific subcommand.

Available subcommands:

[auth]
    changepassword
    createsuperuser

[contenttypes]
    remove_stale_contenttypes

[django]
    check
    compilemessages
    createcachetable
    dbshell
    diffsettings
    dumpdata
    flush
    inspectdb
    loaddata
    makemessages
    makemigrations
    migrate
    optimizemigration
    sendtestemail
    shell
    showmigrations
    sqlflush
    sqlmigrate
    sqlsequencereset
    squashmigrations
    startapp
    startproject
    test
    testserver

[sessions]
    clearsessions

[staticfiles]
    collectstatic
    findstatic
    runserver
(env) PS C:\Users\Manasa\Desktop\first_project&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If this command throws an error, check whether you added '.' while creating the project.&lt;/p&gt;

&lt;p&gt;The next step is optional but &lt;strong&gt;recommended&lt;/strong&gt;. Migrate the initial changes by running the command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; python manage.py migrate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command sets up the default database that Django provides.&lt;/p&gt;

&lt;p&gt;Finally, run the server locally. The following command should give the result.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(env) PS C:\Users\Manasa\Desktop\first_project&amp;gt; python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
August 07, 2022 - 16:21:53
Django version 4.1, using settings 'first_project.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;a href="http://127.0.0.1:8000/"&gt;http://127.0.0.1:8000/&lt;/a&gt;&lt;/strong&gt; is the link to the locally hosted application&lt;/p&gt;

&lt;p&gt;If you see this page on following the link:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Xqx5058T--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7iz8yjc7xusyn5752rjd.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Xqx5058T--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7iz8yjc7xusyn5752rjd.jpg" alt="Django Project Hosted" width="880" height="425"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Your project is successfully set up and ready to run!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>django</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
