How to put static and media setting at the same time in Django

Viewed 2625

Hi i'm quite confuse on this, on how to put static and media in urlpatterns at the same time, I already understand that static is for css/javascript while media is for the user upload file, since then if I use the {% static '<pathname>' %} in html file but i'm referring on the media not on the css/javascript, is there a conflict or how django will understand that i'm targeting the media one?

here is my urls.py

from django.conf import settings
from django.conf.urls.static import static
from django.urls import path
from .views import (
    login,
    signup,
)

urlpatterns = [
    path('', login, name = 'login'),
    path('signup/', signup, name = 'signup')
]

urlpatterns += static(settings.STATIC_URL, document_root = settings.STATICFILES_DIRS)

I already put the static and works perfectly but where I should but the media?

3 Answers

In your urls.py you can do something like this for development

from django.urls import path

urlpatters = [
    # your url patterns here
]

if settings.DEBUG:
    from django.conf.urls.static import static
    from django.contrib.staticfiles.urls import staticfiles_urlpatterns

    # serve static and media files from development server
    urlpatterns += staticfiles_urlpatterns()
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

You don't need to use these settings when you have DEBUG = False in your settings.py file.

Also in your settings.py file, you should set the static files location and media files location because once you deploy this into production, Django will use these settings when you run python manage.py collectstatic. Here is an example:

STATICFILES_STORAGE = 'django.contrib.staticfiles.storage.StaticFilesStorage'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'

=== EDIT to answer your question in the comment ===

When user generated content is uploaded (AKA media), a reference to that file's location is stored in the database. So when your context is passed to the template you will automatically get an image object you can reference within your template.

You would do something like this

<img src="{{ self.someimage.url }}" alt=""> # would give you the url path.

I'm not sure of your context passed to the template, so you would need to take if from here or paste your view from the views.py file in your question.

You can use the STATICFILES_DIRS setting to build up a list of assets and then namespace the "media" folder.

ex:

STATICFILES_DIRS = [
    ("media", "/path/to/media/directory"),  
]

and use it like this in your templates

{% static "media/your_asset_file" %}

Add MEDIA_URL and MEDIA_ROOT in your settings file.

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR,'media')

Then in your urls.py file Simply add these line below the one you have for static files.

urlpatterns+=static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)
Related