Search code examples
pythondjangodjango-templatesdjango-signals

How can i upload Default Image for all new user when the registration form is submitted


is there anyway I can Upload Image for all new user in their profile. I created a user registration page so when a user submitted the form the new profile will be created automatically using Django Signals the problem I'm facing now is: when a new user is created and redirected the user to home page, when a user click on the Profile page the profile page throws an error like this:

ValueError at /profile/15/
The 'profile_image' attribute has no file associated with it.

and it highlighted this html code in the page:

<img class="card-img-top" src="{{profile.profile_image.url}}" alt="Card image cap">

is there anyway I can upload image for all new user in their profile just to avoid the error message in the template. Just like How I make automatic profile for all new user.

my model:

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    profile_image = models.ImageField(upload_to='avatar', blank=True, null=True)
    stories = RichTextField(blank=True, null=True)
    twitter = models.URLField(max_length=300, blank=True, null=True)
    website = models.URLField(max_length=300,blank=True, null=True)
    city = models.CharField(max_length=50, blank=True, null=True)
    location = models.CharField(max_length=80, blank=True, null=True)
    slug = models.SlugField(unique=True, max_length=200)

    def save(self, *args, **kwargs):
        self.slug = slugify(self.user)
        super(Profile, self).save(*args, **kwargs)

    def __str__(self):
        return str(self.user)

signals.py file:

from django.conf import settings 
from django.db.models.signals import post_save
from django.dispatch import receiver
from . models import Profile

User = settings.AUTH_USER_MODEL

#this function wake up after a User object save
@receiver(post_save, sender=User)
def create_profile(sender, instance, created, **kwargs):
    if created:
        Profile.objects.create(user=instance)

profile template:

  <div class="container">
    {% for profile in profiles %}
    <div class="row justify-content-center">
        <div class="card" style="width: 18rem;">
            <img class="card-img-top" src="{{profile.profile_image.url}}" alt="Card image cap">
            <div class="card-body">
                <a href="{% url 'edit' profile.slug %}">Edit</a>
                <a href="{{profile.website}}">My website</a>
                <a href="{{profile.twitter}}">My twitter</a> 
                <p>{{profile.city}}</p>    
                <p>{{profile.location}}</p> 
                <p>{{profile.stories}}</p>            
            </div>
          </div>
    </div>
    {% endfor %}
</div>

my views:

def profile(request, pk):
    profiles = Profile.objects.filter(user=request.user)
    questions = Question.objects.filter(user=request.user)
    context = {
        'profiles':profiles,
        'questions':questions
    }
    return render(request, 'profile.html', context)

def register(request):
    if request.method == 'POST':
        username = request.POST['username']
        first_name = request.POST['first_name']
        last_name = request.POST['last_name']
        email = request.POST['email']
        password = request.POST['password']
        password2 = request.POST['password2']

        if password == password2:
            if User.objects.filter(email=email).exists():
                messages.info(request, 'Email or user name Already taking')
                return redirect('register')
            elif User.objects.filter(username=username).exists():
                messages.info(request, 'username is taken')
                return redirect('register')
            else:
                user = User.objects.create_user(username=username, first_name=first_name, 
                last_name=last_name, email=email, password=password)
                user.save();
                return redirect('login')
        else:
            messages.info(request, 'Password Not Match')
            return redirect('register')   
        return redirect ('/')     
    else:
        return render(request, 'signup.html')

Solution

  • You can take this approach.

    Within the media folder you can add an image called default.png or default.jpg.

    Now within your models.py file, you can have:

    class Profile(models.Model):
         ...
         # setting the default image from here if none was uploaded when creating the profile
         # passing the name of the image to the default attribute
         profile_image = models.ImageField(upload_to='avatar', blank=True, null=True, default='default.jpg')
         ...
    

    Also, you could do a check (from the template) to see if that given profile has an image:

    {% if profile.profile_image %}
         <img class="card-img-top" src="{{ profile.profile_image.url }}" alt="Card image cap">
    {% endif %}
    

    I'd suggest adding both of the above to what you have though.