Skip to content

[BUG] Unable to retrieve media file in development #22

@amserra

Description

@amserra

Hey! Thank you for your work, really appreciate it.

I'm reposting an issue I recently created on the main repo, hope you can help me.

Describe the bug
I have a model like so:

class Organization(models.Model):
    uuid = models.UUIDField(unique=True, default=uuid.uuid4, editable=False)
    logo = models.ImageField(upload_to=ORGANIZATION_LOGO_PATH)

My settings.py has the following MEDIA_URL:

MEDIA_URL = 'media/'

And my main urls.py has the following logic:

urlpatterns = [
    ...
]

# Serve static and media files in development (insecure for production!)
if settings.DEBUG is True:
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

As recommended by the docs.

I want my Ninja endpoint to return the full URL of the image in development so that I can use it in an external application.

This configuration works to display images on Django's templates, and in DRF, given that the request was passed to the context, like so:

serialized_data = OrganizationSerializer(organizations, many=True, context={"request": request}).data

Unfortunately, I am not being able to make it work with Ninja. The docs have no reference to this, and the only stuff I found were these two closed issues:

Which tells me to use the full URL instead of just /media. I've tried that, setting the MEDIA_URL variable to 'http://localhost:8000/media/' or 'http://127.0.0.1:8000/media/', but it does not work, I get a 404.

Help would be appreciated!

Versions:

  • Python version: [e.g. 3.10.0]
  • Django version: [e.g. 5.0.3]
  • Django-Ninja version: [e.g. 1.3.0]
  • Pydantic version: [e.g. 2.10.6]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions