Configure Static & Media Files in Django | Python

Поділитися
Вставка
  • Опубліковано 5 вер 2024
  • In this video, We will learn how we can configure the static files settings. Websites generally need to serve additional files such as images, JavaScript, or CSS. In Django, we refer to these files as “static files”.
    Configuring static files¶
    1. Make sure that "django.contrib.staticfiles" is included in your INSTALLED_APPS.
    2. In your settings file, define STATIC_URL, for example:
    STATIC_URL = 'static/'
    3. In your templates, use the static template tag to build the URL for the given relative path using the configured STATICFILES_STORAGE.
    {% load static %}
    < img src="{% static 'app/example.jpg' %}" alt="My image" >
    4. n addition to using a static/ directory inside your apps, you can define a list of directories (STATICFILES_DIRS) in your settings file where Django will also look for static files. For example:
    STATICFILES_DIRS = [
    BASE_DIR / "static",
    '/var/www/static/',
    ]
    For example, if your STATIC_URL is defined as static/, you can do this by adding the following snippet to your urls.py:
    from django.conf import settings
    from django.conf.urls.static import static
    urlpatterns = [
    ... the rest of your URLconf goes here ...
    ] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
    👍 If you enjoyed this content, give the video a like. If you want to watch more upcoming videos, consider subscribing to the channel! → / @skillshats
    🛝 DJANGO PLAYLIST:
    • Django
    🎬 RECOMMENDED VIDEOS:
    Django Models:
    → • Add record in Django m...
    → • Add record in Django m...
    → • Update & Delete record...
    ䷐ FOLLOW US:
    Website: skillshats.com
    Twitter: / skillshats
    🏷 TAGS:
    #python #django #django-models
    Thanks!!
    Enjoy 🤗

КОМЕНТАРІ • 9

  • @Ks-oj6tc
    @Ks-oj6tc Рік тому +1

    Very well explained. Thanks. Voice is little low.

  • @vedanthimte
    @vedanthimte Рік тому +1

    Nice bro 👍 it helped

  • @rafhaellowixhomwad
    @rafhaellowixhomwad Місяць тому

    I use preline.js for my UI. Once I add the media url, it causes JavaScript to get a 404 not found. Help, I dont know how to fix it.

    • @skillshats
      @skillshats  Місяць тому

      Ensure your Django settings are correctly configured for static files.
      Add STATIC_URL = '/static/' and STATICFILES_DIRS = [BASE_DIR / 'static'] in settings.py. In your template, load the static file using {% load static %} .
      Run python manage.py collectstatic in production.
      For development, use static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) in urls.py.
      This should resolve the 404 error.

    • @rafhaellowixhomwad
      @rafhaellowixhomwad Місяць тому

      @skillshats problem solved. thank you so much. ✨️

  • @n.mariama9454
    @n.mariama9454 Місяць тому

    It's still not working for me

    • @skillshats
      @skillshats  Місяць тому

      What's the issue you are facing?