Romeo
Romeo

Reputation: 788

Default image set in image field not found

In the Profile model below, a default image is set in the image field for a registered user. A default image is provided in the Django media directory media/profile_pics After the register view form is saved(in line 10) in the views, an error message is printed out saying the file or directory to the image is not found. This is because the directory in which Django is looking for the image is wrong. Its looking in the path C:\\dev\\blogger\\media\\default.png instead of C:\\dev\\blogger\\media\\profile_pics\\default.png

I've been on this for hours just trying to fix it, but to no avail.

source code below.

Error:

FileNotFoundError at /account/register

[Errno 2] No such file or directory: 'C:\\dev\\blogger\\media\\default.png

Directory

C:.
├───.vscode
├───blog
│   ├───migrations
│   │   └───__pycache__
│   └───__pycache__
├───blogger
│   ├───static
│   │   └───blog
│   └───__pycache__
├───media
│   └───profile_pics
├───templates
│   ├───blog
│   │   └───post
│   └───users
├───users
│   ├───migrations
│   │   └───__pycache__
│   └───__pycache__

Model:

from django.db import models
from django.contrib.auth.models import User
from PIL import Image

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    image = models.ImageField(default='default.png', upload_to='profile_pics')

    def __str__(self):
        return f'{self.user.username} Profile'

    # Override save nethod. Resize images for scalability
    def save(self, *args, **kwargs):

        super().save(*args, **kwargs)

        img = Image.open(self.image.path)

        if img.height > 300 or img.width > 300:
            resize_img = (300, 300)
            img.thumbnail(resize_img)
            img.save(self.image.path)

        # [TODO] Delete older images when updated with new images

views:

from django.shortcuts import render, redirect
from users.forms import UserRegisterationForm, UserUpdateForm, ProfileUpdateForm
from django.contrib import messages
from django.contrib.auth.decorators import login_required

def register(request):
    if request.method == 'POST':
        form = UserRegisterationForm(request.POST)
        if form.is_valid():
            form.save()
            username = form.cleaned_data.get('username')
            messages.success(request, f'Welcome {username}! You are now able to login.')
            return redirect('account:login')
        else:
            messages.warning(request, 'Oops! Please try again')
            return redirect('account:register')
    else:
        messages.success(request, f'Hi! Fill in fields to join')
        form = UserRegisterationForm()

    return render(request, 'users/register.html', {'form':form})

Upvotes: 1

Views: 435

Answers (1)

Airith
Airith

Reputation: 2164

Specifying just the file will look in your media folder, if your default image is in media/profile_pics then set the default to profile_pics/default.png

Upvotes: 2

Related Questions