简体   繁体   中英

I want to show the Default pic to the user profile whenever a new user is registered using Signals without doing it manually from Django admin

I want to load the default pic of the user, whenever a new user is created using signals. But its not loading the default pic. I don't want to do this manually from Django admin, I want this to be done whenever a new user is created in the app.

models.py

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


class userprofile(models.Model):
    user = models.OneToOneField(User,on_delete = models.CASCADE)
    profilepic = models.ImageField(default='pp.png',upload_to='profile_pic',blank = True)

forms.py

from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm
from .models import userprofile



class ProfileUpdateForm(forms.ModelForm):
    class Meta:
        model = userprofile
        fields = ['profilepic',]

views.py


@login_required
def profile(request):    
    if request.method == 'POST':
        p_form = ProfileUpdateForm(request.POST,request.FILES,instance=request.user.userprofile)
        if p_form.is_valid():
            p_form.save()        
            return render(request,'profile.html')
    else:
        p_form = ProfileUpdateForm(instance=request.user.user)

    context = {
        'p_form': p_form        
        }

    return render(request,'profile.html',context)

profile.html

<form method ="POST" class="sm:w-1/3 text-center sm:pr-8 sm:py-8" enctype="multipart/form-data" >
            {% csrf_token %}
            <img id="profile_pic" alt="team" class="flex-shrink-0 rounded-lg w-full h-56 object-cover object-center mb-4" src="{{user.userprofile.profilepic.url}}">
            {{ p_form|crispy }}
            <br>
            <input id="file-input" style="padding: 8px 93px;" class="text-white bg-green-500 border-0 py-2 px-8 focus:outline-none hover:bg-green-700 rounded text-lg" type="submit" value="Upload">
</form>

signalspy

from django.db.models.signals import post_save
from django.contrib.auth.models import User
from django.dispatch import receiver
from .models import userprofile


@receiver(post_save, sender=User)
def create_profile(sender, instance, created, **kwargs):
    if created:
        userprofile.objects.create(user=instance)


@receiver(post_save, sender=User)
def save_profile(sender, instance, **kwargs):
    instance.profile.save()

apps.py

from django.apps import AppConfig


class HomepageConfig(AppConfig):
    name = 'Homepage'


    def ready(self):
        import users.signals

First thing load the image outside the form. Then add the default image inside media and outside the folder where you upload the profile picture. 在此处输入图像描述

As you can see it is inside the root of media folder.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM