Reputation: 55
Since I have faced this problem many times and couldn't find a solution, I need help
Here's my settings file. I've configured media root
from pathlib import Path
import os
import django_heroku
import django
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.1/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'ws^pgf$%!=l8y#%^7anp$rl6*o4u9!86g-ba_uq9pcee=vc@13'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'home',
]
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 = 'testimage.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR,'templates')],
'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 = 'testimage.wsgi.application'
# Database
# https://docs.djangoproject.com/en/3.1/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
# Password validation
# https://docs.djangoproject.com/en/3.1/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/3.1/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.1/howto/static-files/
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR,"static")]
STATIC_ROOT = os.path.join(BASE_DIR,'staticfiles')
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
django_heroku.settings(locals())
STATICFILES_STORAGE = 'django.contrib.staticfiles.storage.StaticFilesStorage'
And this is a simple models just to add and display images
from django.db import models
# Create your models here.
class Images(models.Model):
photo = models.ImageField(blank=True, null=True, default=None)
title = models.CharField(max_length=250)
def __str__(self):
return self.title
Here's my views.py and the template
from django.shortcuts import render
from home.models import Images
# Create your views here.
def index(request):
images = Images.objects.all()
context = {
'images':images
}
return render(request,'home.html',context)
home.html
{%for i in images %}
<img src="{{i.photo.url}}" alt="">
{%endfor%}
Everytime I try to load that page, I always get this error:
[15/Feb/2021 11:57:12] "GET /media/20210129_205930.jpg HTTP/1.1" 404 2130
[15/Feb/2021 11:57:12] "GET /media/20201227_111422.jpg HTTP/1.1" 404 2130
I know it is telling that there is something wrong with the media path but I checked it all but can't find any wrong point. Please take a look
Upvotes: 0
Views: 516
Reputation: 790
i hope you done all your url.py code as mention above so image is a file so you have to request for file like this
def index(request.FILES, request.POST):
images = Images.objects.all()
context = {
'images':images
}
return render(request,'home.html',context)
and in yours html file form tag use enctype
<form action="{% url 'name' %}" method'POST' enctype="multipart/form-data">
and remove you default from your image class if you want you image can be blank=True no need of default in that and what i can see in your model yo didnt define path whwe you image has to be stored something like this
profile_pic = models.ImageField(upload_to='profile_pics',default='default.png',)
and in your main urls.py add
+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
i hope this will help and tell me if you got any error
Upvotes: 0
Reputation: 2402
It seems you haven't added media urls in your url.py
file, so you can do something like this
# make your imports
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
....
]
# for Debugging
if settings.DEBUG:
urlpatterns = urlpatterns + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns = urlpatterns + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Upvotes: 1