#Problem with static and media directory
14 messages · Page 1 of 1 (latest)
Ok you're right. So even when an admin adds an item through the admin panel, it is not a static image, but should be placed in media/images/... Even though it's not a user-uploaded photo?
There is no problem with the code as it looks to be working as I would expect an ImageField to work.
I would guess that your understanding of static files and media files is incorrect.
Does an admin user upload these images?
User uploaded doesn't distinguish between the user types you might have
Static files live in your codebase, media files get uploaded at runtime.
Admin IS a user, so it's user-uploaded photo
Though I always say that "Media == user uploads" is not really correct
Also how do you serve media files? If you are in debug using runserver it needs manually added url to serve media
Ok, so I just didn't understand:
static: static things that are needed to render the page (css, js, images)
media: which links are created/added to be displayed dynamically?
urlpatterns = [
path('admin/', admin.site.urls),
path('staff/', include('staff.urls', namespace='staff')),
path('', include('users.urls', namespace='users')),
path('', include('shop.urls', namespace='shop'))
]
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Ok, so even if the admin adds any photo to the database, it is a media file and not a static one?
so the upload should looks like this:
def upload_to_path(instance, filename):
category_slug = slugify(instance.category.name)
return f'images/{category_slug}/{filename}'
i remove static/ in the return
static files ~= files created during development process and kept under version control
media files ~= files created during operation of site and might change without developer commits