skip to Main Content

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


  1. Chosen as BEST ANSWER

    it works now after update views.py like this

    img_file = request.FILES.get('imgg')
         try:
         employee = Employee.objects.create(
         img=img_file
    
    
     employee.save()  # This should trigger the print/log statements
    

    list.html

    {% if datas.employee.img %}
                <img src="{{ datas.employee.img.url }}" alt="User Avatar" class="user-avatar">
            {% else %}
                <img src="{% static 'path/to/default/image.png' %}" alt="Default Avatar" class="user-avatar">
            {% endif %}
    
    enter code here
    

  2. In your project root settings.py file

    STATIC_URL = 'static/'
    MEDIA_URL = '/media/'
    MEDIA_ROOT = BASE_DIR / 'media'
    

    In your project root urls.py file

    from django.conf import settings
    from django.conf.urls.static import static
    
    urlpatterns = [
        path('admin/', admin.site.urls),
          # app URLs comes here
    ]
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
    

    In your app models.py file

    import uuid
    
    from django.db import models
    from django.contrib.auth.models import AbstractUser
    from django.utils.translation import gettext_lazy as _
    
    def upload_to(instance, filename):
        return '/'.join([str(instance), filename])
    
    
    
    class TimeStampedModel(models.Model):
        created_at = models.DateTimeField(auto_now_add=True)
        updated_at = models.DateTimeField(auto_now=True)
    
        class Meta:
            abstract = True
    
    
    class User(AbstractUser, TimeStampedModel):
        id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
        first_name = models.CharField(max_length=255, null=True, blank=True)
        last_name = models.CharField(max_length=255, null=True, blank=True)
        full_name = models.CharField(max_length=255, null=True, blank=True)
        email = models.EmailField(unique=True)
        username = models.CharField(max_length=255, unique=True)
        password = models.CharField(max_length=255, null=True, blank=True)
        reset_key = models.CharField(max_length=255, null=True, blank=True)
        profile_image = models.ImageField(upload_to=upload_to, null=True, blank=True)
    

    Now this is how the local server serves your images:

    BASE_URL = http://0.0.0.0:8003
    
    user = User.objects.get(email=email)
    if user.profile_image:
       profile_image = BACKEND_BASE_URL + user.profile_image.url
    

    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 use 127.0.0.1

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search