Py学习  »  Django

使用if condition django将imagefield重置为默认图像

Irfan Harun • 4 年前 • 258 次点击  

我的个人资料表格有两部分:个人和非政府组织(表格的非政府组织部分有imagefield)。用户可以在配置文件类型之间切换。如果用户从非政府组织切换到个人,我会在函数中将非政府组织相关字段更新为NA。但是,我需要将与NGO相关联的图像字段重置为默认值并删除图像。

class Profile(models.Model):
user = models.OneToOneField(User,on_delete=models.CASCADE)
# on_deleting user, profile will also be deleted
image = models.ImageField(default="profilepic.jpg",upload_to="profile_pictures")
profile_type = models.CharField(max_length=20,choices=PROFILE_TYPES, default='individual')
dob = models.DateField(null=True)
bio = models.TextField(null=True)
anonymous = models.BooleanField(default=False)
ngo_name = models.CharField(max_length=200, blank=True)
ngo_address = models.CharField(max_length=200, blank=True)
ngo_phone = models.CharField(max_length=200, blank=True)
ngo_registration_num = models.CharField(max_length=200, blank=True)
ngo_registration_cert = models.ImageField(default="default_ngo.png",upload_to="ngo_documents")
ngo_status = models.BooleanField(default=False)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
def __str__(self):
    return f'{self.user.username} Profile'

表单.py

class ProfileForm(forms.ModelForm):
class Meta:
    model = Profile
    fields = ['image','profile_type','dob','bio','anonymous','profile_type','ngo_name','ngo_address','ngo_phone','ngo_registration_num','ngo_registration_cert','ngo_status']

视图.py

@login_required
def profile_edit(request):
    if request.method == 'POST':
        u_form = UserUpdateForm(request.POST, instance=request.user)
        p_form = ProfileForm(request.POST, request.FILES, instance=request.user.profile)
        if u_form.is_valid() and p_form.is_valid():
            if p_form.cleaned_data.get('profile_type') == 'ngo':
                u_form.save()
                p_form.save()
                messages.success(request, f'your profile has been updated as NGO ')
                return redirect('users:profile')
            else:
                u_form.save()
                temp_p_form = p_form.save(commit=False)
                temp_p_form.ngo_name = 'NA'
                temp_p_form.ngo_address = 'NA'
                temp_p_form.ngo_phone = 'NA'
                temp_p_form.ngo_registration_num = 'NA'
                temp_p_form.ngo_phone = 'NA'
                temp_p_form.ngo_status = False
                temp_p_form.ngo_registration_cert = NEED TO SET THIS TO DEFAULT IMAGE AGAIN
                temp_p_form.save()
                messages.success(request, f'your profile has been updated as Individual ')
                return redirect('users:profile')

    else:
        u_form = UserUpdateForm(instance=request.user)
        p_form = ProfileForm()
    context = {
        'u_form' : u_form,
        'p_form' : p_form
    }
    return render(request, 'users/profile_edit.html',context)
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/56853
 
258 次点击  
文章 [ 1 ]  |  最新文章 4 年前
Таалай ДюшебекоÐ
Reply   •   1 楼
Таалай ДюшебекоР   4 年前

在您的模型中:

image = models.ImageField(default="profilepic.jpg",upload_to="profile_pictures", blank=True, null=True)

temp_p_form.ngo_status = False
temp_p_form.image.delete()
temp_p_form.image = None