在django-template中显示FileField的初始值

时间:2016-05-27 05:11:33

标签: django django-forms

我有一个ModelForm,除了FileField之外,其所有字段的初始值都在模板中正确显示。我想以与django-admin中显示的相同的方式显示该字段的初始值(带currentlyclear复选框)。我该怎么做?

Models.py

class UserDetail(models.Model):
    user = models.OneToOneField(settings.AUTH_USER_MODEL)
    profile_picture = models.FileField(upload_to=profile_image_upload, null=True, blank=True)

    phone = models.CharField(max_length=10)

forms.py

class EditProfileForm(forms.ModelForm):
    class Meta:
        model = UserDetail
        fields = ['profile_picture', 'phone']

views.py

data = {
        'profile_picture' : user_detail.profile_picture,
        'phone' : user_detail.phone,
    }

    context["form"] = EditProfileForm(data)

现在当我{{form}}时,phone的初始值正确显示,但profile_picture没有。它说,No file chosen

如何在django-admin中显示当前当前文件路径和清除复选框?

1 个答案:

答案 0 :(得分:1)

您正在使用ModelForm然后为什么不利用它来显示。?

form = EditProfileForm(instance=your_user_profile_instance)
return render(request, 'test.html', {'form':form})

context["form"] = EditProfileForm(initial=data)