Home > Net >  Having issue in loading files from static folder
Having issue in loading files from static folder

Time:07-11

I am new on django and having issue while following a tutorial on YouTube I stuck in it and try several things but didn't get rid of it plz help me so my static page which i changed into dynamic will load with changes that i made.Everything is working fine except when i try to run it by using {%static%} and i also told the system about static stuff through {%load static%} but it not showing pictures which i try to show through static folder.

  1. List item

Not showing pictures on web page. Having issue to upload it from static folder.

CodePudding user response:

Not sure exactly what your problem is here but, as I understand it, you are trying to use images from your static folder in a django template. This can be achieved by correctly configuring django and using template tags.

In Development

To make this work with the development server, you need to create a folder called static inside each app's folder. For example, your project's file structure could look like this:

project
   |_ project
   |_ app
      |_ static
         |_ app
            |_ image.png
      |_ templates
         |_ app
            |_ home.html
      |_ __init__.py
      |_ admin.py
      |_ models.py
      |_ tests.py
      |_ urls.py
      |_ views.py
   |_ db.sqlite3
   |_ manage.py

Then, you simply access your image from the template home.html by adding {% load static %} at the beginning of the file and using {% static "app/image.png" %} wherever you want to use your image. Note that this template tag basically gives you the url to the static file you want to show. So, actually, you should insert the image like this:

<img src="{% static 'app/image.png' %}" />

In Production

In production, we need to collect all the static files from the different apps in one common place, so that the server can easily find them. We do this with the useful django app staticfiles, which we can configure in the settings.py file.

settings.py

Make sure your settings.py includes the following lines

from pathlib import Path

BASE_DIR = Path(__file__).resolve().parent.parent

INSTALLED_APPS = [
  ...
  'django.contrib.staticfiles',
  ...
]


STATIC_URL = 'static/'
STATIC_ROOT = BASE_DIR / 'staticfiles'

You can replace 'static/' with whatever you want to be your static url. You can replace 'staticfiles' in STATIC_ROOT with the name of the root folder where you will collect your static files. This folder must be (with this configuration) in your project's base directory - the same folder where you have your database (db.sqlite3). For example:

project
   |_ project
   |_ app
   |_ staticfiles
   |_ db.sqlite3
   |_ manage.py

When you run python3 manage.py collectstatic, all the static files of each app of your project will be copied into the STATIC_ROOT directory, so they are all available from the same place. Your file structure will look like:

project
   |_ project
   |_ app
   |_ staticfiles
      |_ app
         |_ image.png
   |_ db.sqlite3
   |_ manage.py

Finally, you will have to tell your production server where the staticfiles directory is, and you are all set to go (how to do this depends on the hosting service you are using).

Hope this helps!

  • Related