社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Django

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

Irfan Harun • 5 年前 • 384 次点击  

我的个人资料表格有两部分:个人和非政府组织(表格的非政府组织部分有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
文章 [ 1 ]  |  最新文章 5 年前