0

我正在尝试从 userprofile.html 模板上传用户的个人资料图片,但它没有上传到管理面板上的该模型。我已经正确配置了静态和媒体设置。我可以从管理面板上传。但是从模板上传不起作用请帮忙..

#forms.py
class ImageUploadForm(forms.ModelForm):
    class Meta:
       model = accountUser
       fields = ['profile_photo',]

#views.py
def upload_pic(request):
  if request.method == 'POST':
      form = ImageUploadForm(request.POST, request.FILES)
      if form.is_valid(): 
         form.save() 
         return redirect('success') 
      else:
         form = ImageUploadForm()

  return HttpResponseForbidden('allowed only via POST')

  #user.html
  <form method = "post" enctype="multipart/form-data"> 
        {% csrf_token %} 
        <p>
           <input id="id_image" type="file" class="" name="image">
        </p>
        <button type="submit">Choose Picture</button> 
  </form>
4

0 回答 0