samedi 29 décembre 2018

Don't upload and load and show Picture profile in Django

Hellow

i use Django freamwork Python. in model i user AbstractBaseUser.

i use python3

in my website user can choice picture for that profile. but don't load and upload picture in my model:

Model:

class CustomUser(AbstractBaseUser , PermissionsMixin):
    username = models.CharField(max_length = 200 , unique = True)
    email = models.EmailField()
    age = models.IntegerField(null=True , blank=True)
    number = models.IntegerField(default=1)
    country = models.CharField(max_length = 200)
    city = models.CharField(max_length = 200)
    image = models.ImageField(upload_to = 'ImageProfile' , default='ImageProfile/none.jpg')
    privilege = models.IntegerField(default= 0)
    is_staff = models.BooleanField(default = False)



    REQUIRED_FIELDS = ['email']
    USERNAME_FIELD = 'username'

    objects = CustomUserManager()

    def get_short_name(self):
        return self.username

    def natural_key(self):
        return self.username

    def __str__(self):
        return self.username

Form:

from django import forms
from django.contrib.auth import get_user_model

class Register(forms.ModelForm):
    password = forms.CharField(widget=forms.PasswordInput)
    password2 = forms.CharField(label='Confirm password', widget=forms.PasswordInput)



    class Meta:
        User = get_user_model()
        model = User
        fields = ('username', 'number' , 'country' , 'city' , 'email' , 'age' , 'image')

    def clean_username(self):
        User = get_user_model()
        username = self.cleaned_data.get('username')
        qs = User.objects.filter(username=username)
        if qs.exists():
            raise forms.ValidationError("username is taken")
        return username

    def clean_password2(self):
        # Check that the two password entries match
        password1 = self.cleaned_data.get("password")
        password2 = self.cleaned_data.get("password2")
        if password1 and password2 and password1 != password2:
            raise forms.ValidationError("Passwords don't match")
        return password2


    def save(self, commit=True):
        # Save the provided password in hashed format
        user = super(Register , self).save(commit=False)
        user.set_password(self.cleaned_data["password"])
        if commit:
            user.save()
        return user

View:

@csrf_exempt
def Home(request):
    if request.method == 'POST':
        form = Register(request.POST)
        if form.is_valid():
            form.password = make_password(form.cleaned_data['password'])
            form = form.save(commit = False)

            form.save()
            return HttpResponse("Greate")
    else:
        form = Register()
        return render_to_response('home.html' , {'form' : form})

TIP : here load and show default picture(none.jpg). if user want to change default don't work

please help me




Aucun commentaire:

Enregistrer un commentaire