Python 在django中编辑表单时,未使用以前的值预先填充字段
我创建了一个表单(EditProfileForm),用于编辑配置文件详细信息。我的问题是,每当我在浏览器中转到EditProfileForm页面时,字段中都没有填写我第一次创建配置文件时给出的先前值,我必须填写整个表单。但是,如果我对值进行了一些更改,则更改成功 我的个人资料模型:Python 在django中编辑表单时,未使用以前的值预先填充字段,python,django,django-models,django-views,django-forms,Python,Django,Django Models,Django Views,Django Forms,我创建了一个表单(EditProfileForm),用于编辑配置文件详细信息。我的问题是,每当我在浏览器中转到EditProfileForm页面时,字段中都没有填写我第一次创建配置文件时给出的先前值,我必须填写整个表单。但是,如果我对值进行了一些更改,则更改成功 我的个人资料模型: class Profile(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE) image = mod
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')
我的两个关于配置文件和编辑配置文件的URL:
@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')
path('profile',user\u views.profile\u页面,name='profile'),
路径('edit_profile/',user_views.edit_profile,name='edit_profile'))
顺便说一句,我第一次使用django信号自动创建配置文件。我使用
ModelForm
的方式是:
form=EditProfileForm(request.POST或None,request.FILES或None,instance=profile)
我不知道这两种说法的区别是什么:
form=EditProfileForm(request.POST或None,request.FILES或None,instance=profile)
及
form=EditProfileForm(request.FILES,instance=profile)
但是,你可以试试这个。
您可以用以下内容替换编辑配置文件(请求,id)
功能:
def编辑_配置文件(请求,id):
profile=profile.objects.get(id=id)
form=EditProfileForm(request.POST或None,request.FILES或None,instance=profile)
如果request.method==“POST”:
如果form.is_有效():
#删除旧上传的图像。
image\u path=profile.image.path
如果os.path.存在(映像路径):
删除操作系统(映像路径)
#'form.save'还将更新最新的图像和路径。
form.save()
返回重定向('配置文件')
返回呈现(请求'blog_app/edit_profile.html',{'profile':profile,'form':form})
在您的应答器中,如果您将相同的值分配给变量表单,即“EditProfileForm(request.POST或None,request.FILES或None,instance=profile)”,那么,如果值相同,为什么要使用if和else条件,为什么要在if条件中传递request.POST,因为它只会在方法不为POST时触发。请回复,我是django的新手,所以可能是我错了。实际上,您可以删除if
else
条件。我在这方面不是很在行。但它对我来说是正确的。是的,你是对的,条件应该删除。我正在更新我的答案。但是,它是否对你有效(可能还有另一个原因我不知道)?我只是尝试了一下,但没有成功