Django Create View Image Upload

前端 未结 3 1871
感动是毒
感动是毒 2021-01-02 19:30

I\'m trying to create a new object in a CreateView via ModelForm. I want the \'player\' instance to have a image. But the uploaded image doesn\'t get stored to the \'player_

相关标签:
3条回答
  • 2021-01-02 20:04

    There might be something missing like request.FILES in views.py or enctype="multipart/form-data" in the form.

    Look here: https://docs.djangoproject.com/en/1.8/topics/http/file-uploads/

    or see this : https://godjango.com/35-upload-files/

    0 讨论(0)
  • 2021-01-02 20:09

    I don't know why it's not working but in my project, which works, I'm getting the image from the post request like so:

    profile_form = ProfileForm(data=request.POST)
    if profile_form.is_valid():
        if 'picture' in request.FILES:
            current_user.image = request.FILES['image']
    
    0 讨论(0)
  • 2021-01-02 20:24

    Go to your template and add:

     <form method="post" enctype="multipart/form-data">
    
    0 讨论(0)
提交回复
热议问题