I am trying to let a user upload and change their picture.
Currently the form displays the current picture for the user without any issues, but I seem to have messed something when it comes to the upload part since the upload is not working (no error messages).
Also, is it possible to create a custom image form? Currently, this makes use of Djangos default one which is not that style-friendly.
Views.py
@login_required
def edit(request):
if request.method == "POST":
FormImage= UserProfileForm(request.POST, request.FILES, instance=request.user)
if FormImage.is_valid():
FormImage.account_id = request.user.id
FormImage.save()
return HttpResponseRedirect(request.path_info)
else:
imageForm = UserProfileForm(instance=request.user)
return render(request, 'accounts/info.html', {
"FormImage": FormImage,
})
Forms.py
class UserProfileForm(UserChangeForm):
FRUIT_CHOICES = (
('Banana', 'Banana'),
('Orange', 'Orange'),
('Apple', 'Apple'),
)
fruits = forms.CharField(widget=forms.Select(choices=FRUIT_CHOICES))
class Meta:
model = Account
fields = (
'fruits',
'profile_picture',
)
Template
<form method="POST" action="" enctype="multipart/form-data">
{% csrf_token %}
<label class="col-lg-3 col-form-label form-coantrol-label">Change picture</label>
<div class="col-lg-9">
{{ FormImage.profile_picture }}
</div>
<div class="form-group row">
<div class="container"><input class="btn btn-primary" type="submit" value="Save profile"></div>
</div>
</form>
My model field for profile pic
profile_picture = models.ImageField(default="default.png", null=True, blank=True)
question from:
https://stackoverflow.com/questions/65836244/django-image-upload-not-posting 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…