Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
224 views
in Technique[技术] by (71.8m points)

Django Image upload not POSTing

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...