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?"
2
Answers
it works now after update views.py like this
list.html
In your project root
settings.py
fileIn your project root
urls.py
fileIn your app
models.py
fileNow this is how the local server serves your images:
now the profile_image contains
image_url
that servers your image, this is how your URL looks like:"profile_image": "http://0.0.0.0:8003/media/b5e6abad-9943-40b0-af14-c6642b8af955/photo-1726706805887-0ac0e0d3a721"
Note: if your server is running on
localhost
then you can use127.0.0.1