santosh acharya
santosh acharya

Reputation: 43

Django: media files doesn't load up uploaded by user in production environment

I'm hosting my site in railway. Everything is set up and works fine but the images uploaded by user don't load up.

settings.py

STATIC_URL = '/static/'

STATICFILES_DIRS = [
    os.path.join(BASE_DIR,'static')
]

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

models.py

class Post(models.Model):
    img = models.ImageField(upload_to="pics")

blog.html

{% extends 'base.html' %}
{% load static %}

{% static "images/projects" as baseUrl %}

{% for post in post_list %}
 <div class="image_wrapper"><a href="{% url 'post_detail' post.slug %}" target="_parent"><img
                    src="{{ post.img.url }}" alt="image 1"/></a></div>

 {% endfor %}

urls.py

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

if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Error I'm getting:

Not Found: /media/pics/CC_Tech_Computers_1_6rgae2m.jpg
Not Found: /media/pics/CC_Tech_Computers_1_6rgae2m.jpg
Not Found: /media/pics/CC_Tech_Computers_1_6rgae2m.jpg
Not Found: /media/pics/CC_Tech_Computers_1_6rgae2m.jpg

Upvotes: 1

Views: 472

Answers (1)

Manoj Tolagekar
Manoj Tolagekar

Reputation: 1970

instead of this:

src="{{ post.img.url }}"

Try this way:

src="/media/pics/post.img"

And in your models.py:

img = models.ImageField(upload_to="pics/") #Here added `forward slash (/)`

Try this way and see if it loads the image

Upvotes: 0

Related Questions