I have the following problem:
When I'm trying to upload an image at the admin panel Fig. 1 and then try to see it I get this error Fig 2
Settings.py:
DEBUG = True
STATIC_URL = '/static/'
MEDIA_URL = '/media/'
MEDIA_ROOT = BASE_DIR / 'media'
portfolio/models.py:
from django.db import models
from django.db.models.fields import CharField, URLField
from django.db.models.fields.files import ImageField
class Project(models.Model):
title = CharField(max_length=100)
description = CharField(max_length=250)
image = ImageField(upload_to="portfolio/images")
url = URLField(blank=True)
portfolio/admin.py:
from django.contrib import admin
from .models import Project
admin.site.register(Project)
my_app/urls.py
from django.contrib import admin
from django.urls import path
urlpatterns = [
path('admin/', admin.site.urls),
]
Thanks :) and sorry for my cool english
CodePudding user response:
Add the following line so django can server media files during development.
urlpatterns = static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
from django.urls import include, path
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
# all url conf
]
if settings.DEBUG:
urlpatterns = static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
CodePudding user response:
In your settings.py file add this:
MEDIA_URL ='/images/'
STATICFILES_DIR = [
BASE_DIR / 'static'
]
MEDIA_ROOT = BASE_DIR / 'static/images'
STATIC_ROOT = BASE_DIR / 'staticfiles'
and then:
In your urls.py inside your project directory add this:
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = static(settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT)