在 django 中编辑表单时未预先填充先前值的字段

时间:2021-04-29 17:43:38

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

我创建了一个表单 (EditProfileForm) 来编辑配置文件详细信息。我的问题是,每当我转到浏览器中的 EditProfileForm 页面时,这些字段都没有填充我在第一次制作配置文件时提供的先前值,我必须填写整个表单,但是如果我对值进行了一些更改则更改成功。

我的个人资料模型:

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    image = models.ImageField(default='profile_pic.jpg', upload_to='profile_pictures') 
    location = models.CharField(max_length=100, blank=True, null=True)
    bio = models.CharField(max_length=500, blank=True, null=True)  

    def __str__(self):
        return self.user.username

我在 forms.py 中的 EditProfileForm:

class EditProfileForm(forms.ModelForm):
 
    class Meta:
        model = Profile
        fields = ['image', 'location', 'bio']

我对个人资料和编辑个人资料的两种看法:

@login_required
def profile_page(request):
    user = request.user
    posts = Post.objects.filter(author=user)
    posts_count = posts.count()
    profile = Profile.objects.get(user=user)
    return render(request, 'blog_app/profile.html', {'user': user, 'posts_count': posts_count, 'profile': profile})

def edit_profile(request, id):
    profile = Profile.objects.get(id=id)
    if request.method == 'GET':
        form = EditProfileForm(request.FILES, instance=profile)
    else:
        form = EditProfileForm(request.POST, request.FILES, instance=profile)
        if form.is_valid():

            # deleting old uploaded image.
            image_path = profile.image.path
            if os.path.exists(image_path):
                os.remove(image_path)

            # the `form.save` will also update the newest image & path.
            form.save()
            return redirect('profile')
                 
    return render(request, 'blog_app/edit_profile.html', {'profile': profile, 'form': form})

我的两个关于个人资料和编辑个人资料的网址:

path('profile', user_views.profile_page, name='profile'),
path('edit_profile/<int:id>', user_views.edit_profile, name='edit_profile')

顺便说一下,我第一次使用 django 信号自动创建配置文件。

1 个答案:

答案 0 :(得分:0)

我使用 ModelForm 的方式是:

form = EditProfileForm(request.POST or None, request.FILES or None, instance=profile)

我不知道这两个陈述之间的实际区别是什么:

form = EditProfileForm(request.POST or None, request.FILES or None, instance=profile)

form = EditProfileForm(request.FILES, instance=profile)

但是,你可以试试这个。 您可以将 edit_profile(request, id) 函数替换为:

def edit_profile(request, id):
    profile = Profile.objects.get(id=id)
    form = EditProfileForm(request.POST or None, request.FILES or None, instance=profile)
    if request.method =='POST':
        if form.is_valid():

            # deleting old uploaded image.
            image_path = profile.image.path
            if os.path.exists(image_path):
                os.remove(image_path)

            # the `form.save` will also update the newest image & path.
            form.save()
            return redirect('profile')
                 
    return render(request, 'blog_app/edit_profile.html', {'profile': profile, 'form': form})
相关问题