I am working on the Django tutorial by W3 Schools. I have got up to where I customize the 404 page. I wanted to add images to the member details page. So I went to the tutorial by Code with Stein to see how he added images to his Item page. I have added the MEDIA_URL, and the MEDIA_ROOT. When I go to my dashboard I am able to upload the image. But when I try to refresh so I see the names of the objects I get a Server Error (500).
my models.py file
from django.db import models
class Member(models.Model):
firstname = models.CharField(max_length=255)
lastname = models.CharField(max_length=255)
phone = models.IntegerField(null=True)
joined_date = models.DateField(null=True)
image = models.ImageField(upload_to='member_images', blank=True, null=True)
def __str__(self):
return self.name
the views.py file
# from django.shortcuts import render
from django.http import HttpResponse
from django.template import loader
from .models import Member
def members(request):
mymembers = Member.objects.all().values()
template = loader.get_template('all_members.html')
# template = loader.get_template('myfirst.html')
# return HttpResponse("Hello world. Say 'Congratulations' to Zack Amata!!")
context = {
'mymembers': mymembers,
}
return HttpResponse(template.render(context, request))
def details(request, id):
mymember = Member.objects.get(id=id)
template = loader.get_template('details.html')
context = {
'mymember': mymember,
}
return HttpResponse(template.render(context, request))
def main(request):
template = loader.get_template('main.html')
return HttpResponse(template.render())
the static section of the settings.py file
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/5.1/howto/static-files/
STATIC_URL = 'static/'
MEDIA_URL = 'media/'
MEDIA_ROOT = BASE_DIR / 'media'