Images do not appear on a page even though they are already in a DB (Django)
I try to learn how to deploy my django project. I have a form in which user can upload his image. The image is a content for ImageField of a Recipe model. Uploading process is working as expected but when I try to display these pictures on the page they just dont appear. Here's my code:
settings.py:
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'
MEDIA_URL = "/media/"
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
urls.py (project one):
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('auth/', include('user_auth.urls')),
path('main/', include('main_app.urls')),
]
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
models.py:
class Recipe(models.Model):
author = models.ForeignKey(UserExtended, on_delete=models.CASCADE, related_name='recipies')
title = models.CharField(max_length=150, null=False)
category = models.ForeignKey(RecipeCategory, related_name='recipies', on_delete=models.CASCADE, null=True)
image = models.ImageField(upload_to="images/", null=False)
short_description = models.CharField(max_length=250, null=False)
tutorial = models.TextField(null=False, default='')
uploaded_on = models.DateTimeField(auto_now_add=True)
likes = models.ManyToManyField(UserExtended, related_name='liked_recipies')
likes_count = models.SmallIntegerField(default=0)
def __str__(self):
return f'{self.author} {self.title}'
example of my template:
<img class="full-recipe-image" src="{{ recipe.image.url }}" alt="Фото блюда">
Any help would be a blessing to me since I'm pretty desperate.
I tried to alter my urls.py and was struggling with MEDIA_ROOT / MEDIA_URL problem
Problem solved! In my settings.py MEDIA_URL should be real Internet URL.