October 25, 2024
Chicago 12, Melborne City, USA
HTML

The image is not being stored in the media folder in Django, but the database contains the image name


views.py

  employee = Employee.objects.create(
user=request.user,  # Assigning the current user
first_name=request.POST.get('employee_firstname'),
middle_name=request.POST.get('employee_middlename'),
last_name=request.POST.get('employee_lastname'),
email=request.POST.get('employee_email'),
land_phone_number=request.POST.get('employee_landphone'),
mobile_phone_number=request.POST.get('employee_mobile'),
gender=request.POST.get('gender'),
hire_date=request.POST.get('hire_date'),
position=position,
address=address,
date_of_birth=request.POST.get('dob'),
img=request.FILES.get('imgg'),  # Make sure you're using request.FILES for image files

)

models.py

class Employee(models.Model):
img = models.ImageField(upload_to='pics')

settings.py

    STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')

STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),

]

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

`# Define Media URL

`MEDIA_URL = '/media/'

urls.py

urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

add_user.html

<div class="contact-img">
<input type="file" id="imgg" name="imgg" class="form-control" accept="image/*">

list.html

<img src="{{ datas.employee.img.url }}" alt="User Avatar" class="user-avatar">

"Why is this not being stored in the media/pics folder?"



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video