imagefield的默认图像不存储默认图像,我也不知道为什么

时间:2019-12-31 04:40:25

标签: python database django-models django-forms django-views

在我的注册表中有一个ImageField,它将在我指定不需要的地方询问个人资料图片。在模型的imagefield中,当用户未选择配置文件图像时,我为图像配置文件指定了默认路径,但是当帐户存储在数据库中时,在我所说的情况下,它不存储默认图像。 Screenshot from the directory where you can find the path of the default profile image

PD:我已经设置了媒体目录的url和root

forms.py

class RegisterForm(forms.Form):
    GENDER = (
        ("M","Hombre"),
        ("F","Mujer"),
    )
    ad_profile=forms.ImageField(label="profile image",widget=forms.FileInput(attrs={'class':'form-control-file',}),validators=[validators.validate_image_file_extension,],required=False)
    ad_name=forms.CharField(label="name",max_length=60,min_length=15,widget=forms.TextInput(attrs={'class':'form-control',}),required=True)
    ad_email=forms.EmailField(label="email",widget=forms.EmailInput(attrs={'class':'form-control','placeholder': 'ejemplo@correo.com'}),required=True,validators=[validators.validate_email,])
    ad_password=forms.CharField(label="password",widget=forms.PasswordInput(attrs={'class':'form-control','placeholder':'Contraseña'}),required=True,min_length=6)
    ad_password_s=forms.CharField(label="password",widget=forms.PasswordInput(attrs={'class':'form-control','placeholder':'Confirmar Contraseña'}),required=True,min_length=6)
    ad_username=forms.CharField(min_length=6,max_length=30,widget=forms.TextInput(attrs={'class':'form-control','placeholder':'user1234'}),required=True)
    ad_genre=forms.ChoiceField(choices=GENDER,widget=forms.Select(attrs={'class':'form-control w-auto'}),required=True)

models.py

class QL_FREE_DT(models.Model):
    GENDER = (
        ("M","Hombre"),
        ("F","Mujer"),
    )
    ql_free_img_link=models.ImageField(upload_to="freelance-profile-picture",default='qtz_default/img/img_avatar.png')
    ql_free_name=models.CharField(max_length=60)
    ql_free_email=models.EmailField(max_length=254,unique=True)
    ql_free_pass=models.CharField(max_length=254)#hash256 max_length=64 bcrypt hash256 lenght=50
    ql_free_username=models.CharField(max_length=30,unique=True)
    ql_free_gender = models.CharField(max_length=1, choices=GENDER, default="M")

    ql_free_desc=models.TextField(max_length=500,blank=True)
    ql_free_created=models.DateTimeField(auto_now_add=True)

views.py

def register(request):
    custom_errors={}
    if(request.method=="POST"):
        form=RegisterForm(request.POST,request.FILES)
        if(form.is_valid()):
            if(form.cleaned_data['ad_password'] == form.cleaned_data['ad_password_s']):
                request.session['qtz-email-storage']=form.cleaned_data['ad_email']
                request.session['qtz-remember-storage']=True
                db_model=QL_FREE_DT(ql_free_img_link=form.cleaned_data['ad_profile'],ql_free_name=form.cleaned_data['ad_name'],
                    ql_free_email=form.cleaned_data['ad_email'],ql_free_pass=make_password(form.cleaned_data['ad_password']),
                    ql_free_username=form.cleaned_data['ad_username'],ql_free_gender=form.cleaned_data['ad_genre'])
                db_model.save()
                return HttpResponseRedirect('/login')
            else:
                custom_errors['dif-passwords']="Contraseñas no coinciden"
        print(form.cleaned_data)
    else:
        form=RegisterForm()
    return render(request,'core/register.html',{'form':form,'custom_errors':custom_errors})

0 个答案:

没有答案