I'm creating a website for an e-sports team where I want the administrators to have the option in backoffice (django admin) to add their collaborating partners. The model is gonna have an image, a link and a description. I know this question is being asked a lot - but trust me I've looked and tried for hours, there must be some minor issue that's been interrupting me.
I can get all the objects to show, with working links and descriptions, but the image wont show up. I sat all day yesterday trying to fix stuff.
I've tried changing the MEDIA_ROOT and MEDIA_URL as well as using different strategies that I've scouted on the internet and here on StackOverflow. But I can't get it to work.
# My model (models.py)
class Partner(models.Model):
""" Adds a partner with specific information """
...
partner_image = models.ImageField(upload_to='partners/', blank=True, null=True)
# Urls (urls.py)
from django.urls import path
from django.conf import settings
from django.contrib.staticfiles.urls import static
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from . import views
urlpatterns = [
path('', views.index, name='index'),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += staticfiles_urlpatterns()
# Appropriate settings (settings.py)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
# Pages (views.py)
def index(request):
""" Home """
news_list = Article.objects.filter(status='p') # only show published news
about_page = About.objects.filter()[:1].get()
partners = Partner.objects.all()
return render(request, 'SVEC/index.html', {'news_list': news_list, 'about_page': about_page, 'partners': partners})
# My template (partners.html, a section within my index.html)
{% for partner in partners %}
<div class="col-md-3 col-sm-6">
<a href="{{ partner.website }}">
<img class="img-fluid d-block mx-auto img-opacity text-just" src="{{ partner.partner_image.url }}" alt=" " data-content="{{ partner.information }}" rel='popover' data-placement='bottom' data-original-title='{{ partner.name }}' data-trigger='hover'>
</a>
</div>
{% endfor %}
When inspecting the element on my web browser, the expected outcome is for each image to show along with their description (while hovering) and possibility to go to the website by clicking.
This is what shows when inspecting, but still no picture showing. Is it some issue with how static files and media files work differently in how they find the correct pathing?
src="/media/partners/msi.png"
UPDATE:
This is what my cmd says, to compare media files with static files:
[03/Jun/2019 12:27:23] "GET /media/partners/msi.png HTTP/1.1" 404 2117
[03/Jun/2019 12:27:23] "GET /static/img/portfolio/01-full.jpg HTTP/1.1" 200 52848
I know Django recognizes the pathing for static files, it might be something different regarding media files?