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

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

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

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 9078

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 60k
Author
Asked: November 28, 20242024-11-28T07:04:07+00:00 2024-11-28T07:04:07+00:00

Django Images: Static vs Media

  • 60k

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.

The 2 ways of displaying images on a web page are:

  • Static images

  • Upload through media


Static Images

If an image is to be displayed from a definite set of images, static method should be used.

Your folder structure inside the project folder must look something like this.

ª   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 
Enter fullscreen mode Exit fullscreen mode

Create a folder in myapp and name it 'static'.

myapp folder structure should look like this.

+---myapp     ª   admin.py     ª   apps.py     ª   models.py     ª   tests.py     ª   views.py     ª   __init__.py     ª        +---migrations     ª       __init__.py     ª     +---static     ª     +---templates     ª       index.html 
Enter fullscreen mode Exit fullscreen mode

Now, in static folder, you can create a folder 'images' and upload your images.

In your specified html file (index.html in this case), add the following at the top of the page:

{% load static %}

In the image tag of the file, add the path to the image by adding the following:

{% load static %} <!DOCTYPE html> <html lang="en"> <head>     <meta charset="UTF-8">     <meta http-equiv="X-UA-Compatible" content="IE=edge">     <meta name="viewport" content="width=device-width, initial-scale=1.0">     <title>Show Static Image</title> </head> <body>     <img src="{% static 'images/image.jpg' %}" alt=""> </body> </html> 
Enter fullscreen mode Exit fullscreen mode

Since static url is already defined in settings.py, the static tag refers to the static folder created.

You can display the image like this:

Static image displayed on web page


Upload Image through Media

When multiple images need to be stored in the database or object or user specific images need to be displayed, this method is used.

The images can be uploaded to the database through:

  • A seperate form on a web page that takes image files

  • Uploading images through admin panel

Here, I will be uploading the images through the admin panel.

Since this method requires Pillow (python module), run the command:
pip install Pillow

Create your model and create an image field.

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/') 
Enter fullscreen mode Exit fullscreen mode

This automatically creates a folder 'media' and a folder 'images' in it once the setup is completed. Migrate the changes.

In urls.py, mention the root to media:

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) 
Enter fullscreen mode Exit fullscreen mode

Lastly, in settings.py, insert:

import os  MEDIA_ROOT =  os.path.join(BASE_DIR, 'media') MEDIA_URL = '/media/' 
Enter fullscreen mode Exit fullscreen mode

Now that the setup is completed, you can insert the data.

To access the admin panel, create a superuser by running the command:
python manage.py createsuperuser

Now register your model in the admin panel by adding the following to admin.py:

from .models import *  admin.site.register(MyModel) 
Enter fullscreen mode Exit fullscreen mode

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.

# Create your views here. def index(request):     images = MyModel.objects.filter(name='manasa')     return render(request, 'index.html', {'images': images}) 
Enter fullscreen mode Exit fullscreen mode

And edit your html page to display these images.

<!DOCTYPE html> <html lang="en"> <head>     <meta charset="UTF-8">     <meta http-equiv="X-UA-Compatible" content="IE=edge">     <meta name="viewport" content="width=device-width, initial-scale=1.0">     <title>Show Media Image</title> </head> <body>     {% for img in images %}     <img src="{{img.image.url}}" alt="" width="200px" height="150px">     {% endfor %} </body> </html> 
Enter fullscreen mode Exit fullscreen mode

Now that the procedure is completed, run the server. The web page should look like this:
Images displayed through media


djangoimagepythonwebdev
  • 0 0 Answers
  • 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

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 1k
  • Popular
  • Answers
  • Author

    How to ensure that all the routes on my Symfony ...

    • 0 Answers
  • Author

    Insights into Forms in Flask

    • 0 Answers
  • Author

    Kick Start Your Next Project With Holo Theme

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

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.