图片已上传但未保存在媒体和数据库Django中

编程入门 行业动态 更新时间:2024-10-26 20:25:25
本文介绍了图片已上传但未保存在媒体和数据库Django中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在创建设置页面时,用户应该可以在那里更改图像和生物数据.但是当我打印请求时,只有生物数据得到更新.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?

Models.py

Models.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中

本文发布于:2023-10-19 18:18:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1508363.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:但未   上传   数据库   媒体   图片

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!