I don't know why I can't show images in my Django project , I am going throght a course and I came across to this problem, the problem is that the program can't find the images that are stored in static/images folder and i don't know what is the problem excatly I added anything that i thought is necessary can you help me to solve this problem ???
models.py
class Project(models.Model):
objects = None
title = models.CharField(max_length=200)
description = models.TextField(null=True, blank=True)
image = models.ImageField(null=True, blank=True, default='default.jpg')
demo_link = models.CharField(null=True, blank=True, max_length=2000)
source_link = models.CharField(null=True, blank=True, max_length=2000)
created = models.DateTimeField(auto_now_add=True)
id = models.UUIDField(default=uuid.uuid4, unique=True, primary_key=True, editable=False)
tag = models.ManyToManyField('Tag', blank=True)
total_vote = models.IntegerField(default=0, null=True, blank=True)
vote_ratio = models.IntegerField(default=0, null=True, blank=True)
def __str__(self):
return self.title
class Review(models.Model):
VOTE = (
('up', 'Up Vote'),
('down', 'Down Vote'),
)
project = models.ForeignKey(Project, on_delete=models.CASCADE)
body = models.TextField(null=True, blank=True)
value = models.CharField(max_length=200, choices=VOTE)
created = models.DateTimeField(auto_now_add=True)
id = models.UUIDField(default=uuid.uuid4, unique=True, primary_key=True, editable=False)
def __str__(self):
return self.value
class Tag(models.Model):
name = models.CharField(max_length=200)
created = models.DateTimeField(auto_now_add=True)
id = models.UUIDField(default=uuid.uuid4, unique=True, primary_key=True, editable=False)
def __str__(self):
return self.name
urls.py
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('', include("projects.urls"))
]
urlpatterns += static(settings.MEDIA_ROOT, document_root=settings.MEDIA_ROOT)
html file
{% include 'navbar.html' %}
<img src="{{obj.image.url}}">
{% for tag in obj.tag.all %}
<span style="border:1px solid grey">{{tag}}</span>
{% endfor %}
<hr>
<h5>Description</h5>
<p>{{obj.description}}</p>
{% include 'footer.html' %}
setting.py
BASE_DIR = Path(__file__).resolve().parent.parent
DEBUG = True
ALLOWED_HOSTS = []
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'projects.apps.ProjectsConfig',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'devsearch.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join(BASE_DIR, 'template'),
],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'devsearch.wsgi.application'
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
...
STATIC_URL = '/static/'
MEDIA_URL = '/images/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static')
]
MEDIA_ROOT = os.path.join(BASE_DIR, 'static/images')
views.py
def projects(request):
objs = Project.objects.all()
context = {"projects": objs}
return render(request, 'projects/project.html', context)
def dynamic(request, text):
obj = Project.objects.get(id=text)
return render(request, 'projects/single-project.html', {'obj': obj})
def createproject(request):
form = ProjectForm()
if request.method == 'POST':
form = ProjectForm(request.POST)
print(request.POST)
if form.is_valid():
form.save()
return redirect('projects')
context = {'form': form}
return render(request, 'projects/projectform.html', context)
def updateproject(request, pk):
project = Project.objects.get(id=pk)
form = ProjectForm(instance=project)
if request.method == 'POST':
form = ProjectForm(request.POST, instance=project)
if form.is_valid():
form.save()
return redirect('projects')
context = {'form': form}
return render(request, 'projects/projectform.html', context)
def deleteproject(request, pk):
project = Project.objects.get(id=pk)
if request.method == 'POST':
project.delete()
return redirect('projects')
context = {'object': project}
return render(request, 'projects/delete_project.html', context)
Traceback
Django version 4.1, using settings 'devsearch.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
[05/Sep/2022 15:25:36] "GET /projects/1d3e224e-792a-4908-ba5b-7133653c67aa HTTP/1.1" 200 507
Not Found: /images/default.jpg
[05/Sep/2022 15:25:36] "GET /images/default.jpg HTTP/1.1" 404 3267
[05/Sep/2022 15:25:37] "GET /projects/1d3e224e-792a-4908-ba5b-7133653c67aa HTTP/1.1" 200 507
Not Found: /images/default.jpg
[05/Sep/2022 15:25:37] "GET /images/default.jpg HTTP/1.1" 404 3267
[05/Sep/2022 15:25:38] "GET /projects/1d3e224e-792a-4908-ba5b-7133653c67aa HTTP/1.1" 200 507
Not Found: /images/default.jpg
[05/Sep/2022 15:25:38] "GET /images/default.jpg HTTP/1.1" 404 3267
[05/Sep/2022 15:25:38] "GET /projects/1d3e224e-792a-4908-ba5b-7133653c67aa HTTP/1.1" 200 507
Not Found: /images/default.jpg
[05/Sep/2022 15:25:38] "GET /images/default.jpg HTTP/1.1" 404 3267
F:\tuts\Django\Exercises\Course\first_project\devsearch\projects\models.py changed, reloading.
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
September 05, 2022 - 15:26:43
Django version 4.1, using settings 'devsearch.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
[05/Sep/2022 15:26:53] "GET /projects/1d3e224e-792a-4908-ba5b-7133653c67aa HTTP/1.1" 200 507
Not Found: /images/default.jpg
[05/Sep/2022 15:26:53] "GET /images/default.jpg HTTP/1.1" 404 3267
F:\tuts\Django\Exercises\Course\first_project\devsearch\devsearch\settings.py changed, reloading.
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
September 05, 2022 - 15:27:15
Django version 4.1, using settings 'devsearch.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
[05/Sep/2022 15:27:16] "GET /projects/1d3e224e-792a-4908-ba5b-7133653c67aa HTTP/1.1" 200 507
Not Found: /images/default.jpg
[05/Sep/2022 15:27:16] "GET /images/default.jpg HTTP/1.1" 404 3267
F:\tuts\Django\Exercises\Course\first_project\devsearch\devsearch\settings.py changed, reloading.
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
September 05, 2022 - 15:27:41
Django version 4.1, using settings 'devsearch.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
Exception in thread django-main-thread:
Traceback (most recent call last):
File "C:\Users\CCnoor\AppData\Local\Programs\Python\Python310\lib\threading.py", line 1016, in _bootstrap_inner
self.run()
File "C:\Users\CCnoor\AppData\Local\Programs\Python\Python310\lib\threading.py", line 953, in run
self._target(*self._args, **self._kwargs)
File "F:\tuts\Django\Exercises\Course\first_project\devsearch\env\lib\site-packages\django\utils\autoreload.py", line 64, in wrapper
fn(*args, **kwargs)
File "F:\tuts\Django\Exercises\Course\first_project\devsearch\env\lib\site-packages\django\core\management\commands\runserver.py", line 157, in inner_run
handler = self.get_handler(*args, **options)
File "F:\tuts\Django\Exercises\Course\first_project\devsearch\env\lib\site-packages\django\contrib\staticfiles\management\commands\runserver.py", line 35, in get_handler
return StaticFilesHandler(handler)
File "F:\tuts\Django\Exercises\Course\first_project\devsearch\env\lib\site-packages\django\contrib\staticfiles\handlers.py", line 75, in __init__
self.base_url = urlparse(self.get_base_url())
File "F:\tuts\Django\Exercises\Course\first_project\devsearch\env\lib\site-packages\django\contrib\staticfiles\handlers.py", line 30, in get_base_url
utils.check_settings()
File "F:\tuts\Django\Exercises\Course\first_project\devsearch\env\lib\site-packages\django\contrib\staticfiles\utils.py", line 63, in check_settings
raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: runserver can't serve media if MEDIA_URL is within STATIC_URL.
F:\tuts\Django\Exercises\Course\first_project\devsearch\devsearch\settings.py changed, reloading.
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
September 05, 2022 - 15:28:15
Django version 4.1, using settings 'devsearch.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
[05/Sep/2022 15:28:18] "GET /projects/1d3e224e-792a-4908-ba5b-7133653c67aa HTTP/1.1" 200 507
Not Found: /images/default.jpg
[05/Sep/2022 15:28:18] "GET /images/default.jpg HTTP/1.1" 404 3267
[05/Sep/2022 15:28:19] "GET /projects/1d3e224e-792a-4908-ba5b-7133653c67aa HTTP/1.1" 200 507
Not Found: /images/default.jpg
[05/Sep/2022 15:28:19] "GET /images/default.jpg HTTP/1.1" 404 3267
[05/Sep/2022 15:28:50] "GET /admin/projects/project/51258114-993e-4d46-ac5d-6046a72723b0/change/ HTTP/1.1" 200 11828
[05/Sep/2022 15:28:50] "GET /admin/jsi18n/ HTTP/1.1" 200 3343
[05/Sep/2022 15:29:01] "GET /projects/51258114-993e-4d46-ac5d-6046a72723b0 HTTP/1.1" 200 512
Not Found: /images/django-react-course.jpg
[05/Sep/2022 15:29:01] "GET /images/django-react-course.jpg HTTP/1.1" 404 3303
[05/Sep/2022 15:29:09] "GET /projects/51258114-993e-4d46-ac5d-6046a72723b0 HTTP/1.1" 200 512
Not Found: /images/django-react-course.jpg
[05/Sep/2022 15:29:09] "GET /images/django-react-course.jpg HTTP/1.1" 404 3303
F:\tuts\Django\Exercises\Course\first_project\devsearch\devsearch\settings.py changed, reloading.
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
September 05, 2022 - 15:30:12
Django version 4.1, using settings 'devsearch.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
[05/Sep/2022 15:30:15] "GET /projects/51258114-993e-4d46-ac5d-6046a72723b0 HTTP/1.1" 200 512
Not Found: /images/django-react-course.jpg
[05/Sep/2022 15:30:15] "GET /images/django-react-course.jpg HTTP/1.1" 404 3303
[05/Sep/2022 15:30:16] "GET /projects/51258114-993e-4d46-ac5d-6046a72723b0 HTTP/1.1" 200 512
Not Found: /images/django-react-course.jpg
[05/Sep/2022 15:30:16] "GET /images/dj
This would help at least upcoming error finder's after you host it to production that time you might change DEBUG = False That time you might end up with some error as the image from database might not be visible. so that
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
.....
]
# solution
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_ROOT, document_root=settings.MEDIA_ROOT)