gpt4 book ai didi

html - 如何在django中显示编辑后的头像

转载 作者:行者123 更新时间:2023-11-28 00:58:48 24 4
gpt4 key购买 nike

我可以上传个人资料图片,但我无法编辑它

我在表单中填充了图像浏览选项,但是当我单击“保存”时,它实际上并没有在模板中更新。但它在管理员中运行良好

View .py

def create_form(request):
form = AlbumForm(request.POST or None, request.FILES or None)
if form.is_valid():
album = form.save(commit=False)
album.user = request.user
album.image= request.FILES['image']
album.save()
return render(request, 'about.html', {'album': album})
context = {
"form": form,
}
return render(request, 'create_form.html', context)

def profile_edit(request):
if request.method == 'POST':
form = AlbumForm(request.POST, instance=request.user.album)
if form.is_valid():
album = form.save(commit=False)
album.user = request.user
form.save()
return redirect(reverse('about'))
else:
form = AlbumForm(instance=request.user.album)
args = {'form': form}
return render(request, 'profile_edit.html', args)

模型.py

class Album(models.Model):
user = models.OneToOneField(User)
image = models.FileField()

表单.py

class AlbumForm(forms.ModelForm):
class Meta:
model = Album
fields = ['image']

profile_edit.html

{%  extends 'base.html' %}
{% block content %}

<form action="{% url 'profile_edit' %}" method="post" enctype="multipart/form-data">
{% csrf_token %}
{{ form.as_p }}
<button type="submit" class="save btn btn-default">save</button>
</form>
<div>
<img src="{{ user.album.image.url }}" class="img-responsive">
</div>

{% endblock %}

最佳答案

试试这个

AlbumForm(request.POST, request.FILES, instance=request.user.album)

关于html - 如何在django中显示编辑后的头像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43206849/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com