图像已上传但未保存在媒体和数据库 Django 中 [英] Image uploaded but not saved in media and database Django
本文介绍了图像已上传但未保存在媒体和数据库 Django 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在创建一个设置页面,用户应该能够在其中更改图像和生物数据.但是只有在我打印 request.FILES.GET(profile_picture") 时才会更新生物数据,它会打印我上传的照片的名称.为什么不上传保存到数据库中?
Am creating a settings page where the user should be able to chang there image and biodata. But only the biodata gets updated when I print the request.FILES.GET("profile_picture") it prints the name of the photo I uploaded. why is it not uploaded and saved to the database?
模型.py
from django.db import models
from django.contrib.auth.models import AbstractUser
class User(AbstractUser):
profile_pic = models.ImageField(upload_to="images/", null=True ,blank=True)
bio = models.TextField(max_length=100, blank=True)
forms.py
class ProfileForm(forms.Form):
profile_picture = forms.ImageField(required=False )
bio = forms.CharField(label='Bio', max_length=100 ,widget=forms.Textarea())
views.py
def settings(request):
if request.user.is_authenticated:
intial_data = {
'first_name': request.user.first_name,
'last_name': request.user.last_name,
'user_name': request.user.username,
'email':request.user.email,
'bio':request.user.bio
}
profile_form=ProfileForm(intial_data)
if request.method == "POST":
profile_form=ProfileForm(request.POST, request.FILES)
if profile_form.is_valid():
user_name = profile_form.cleaned_data.get('user_name')
profile_picture =request.FILES.get('profile_picture')
bio = profile_form.cleaned_data.get('bio')
user= User.objects.get(pk=request.user.id)
if user is not None:
user.profile_picture=profile_picture
user.bio=bio
user.save()
context ={
"profile_form":profile_form
}
return render(request, 'user/settings.html',context)
return redirect('home')
my_template.html
my_template.html
<tr>
<td>Bio</td>
<td>{{profile_form.bio}}</td>
</tr>
<tr>
<td>Update Profile Picture</td>
<td>{{profile_form.profile_picture}}</td>
</tr>
推荐答案
行中有一个尾随逗号:
# trailing comma ↓
user.profile_picture=profile_picture,
您需要删除它.
话虽如此,我还是建议使用 ModelForm
,这样可以删除很多样板代码:
That being said, I would advice to use a ModelForm
, this can remove a lot of boilerplate code:
class ProfileForm(forms.Form):
class Meta:
model = User
fields = ['profile_pic', 'bio']
labels = {
'profile_pic': 'Profile picture'
}
widgets = {
'bio': forms.Textarea
}
然后在视图中,我们可以使用:
Then in the view, we can use:
from django.contrib.auth.decorators import login_required
@login_required
def settings(request):
if request.method == 'POST':
profile_form = ProfileForm(request.POST, request.FILES, instance=request.user)
if profile_form.is_valid():
profile_form.save()
return redirect('home')
else:
profile_form = ProfileForm(instance=request.user)
context ={
'profile_form': profile_form
}
return render(request, 'user/settings.html', context)
这篇关于图像已上传但未保存在媒体和数据库 Django 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文