不要在Django中上传,加载和显示图片个人资料

时间:2018-12-29 12:31:53

标签: python django python-3.x web web-development-server

Hellow

我使用Django freamwork Python。在模型中,我用户AbstractBaseUser。

我使用python3

我网站上的

用户可以为该个人资料选择图片。但不要在我的模型中加载和上传图片:

型号:

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

表格:

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

查看:

@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})

提示:在此处加载并显示默认图片(none.jpg)。如果用户要更改默认设置不起作用

请帮助我

0 个答案:

没有答案