Django form upload request.files empty -
I have posted a question here before, mostly read.
I'm learning that Django and file upload has worked before but now I have broken it in some way
request.FILES is empty when IM is uploading but I I can see the file name in request.raw_post_data.
Here is the html code It will be very easy to do what you are trying to do using ModelForms:
& lt; Form enctype = "multipart / form-data" method = "post" action = "" & gt; {% Csrf_token%} {{form.as_p}} & lt; Input type = "submit" name = "submit" value = "photo upload" / & gt; (Max_length = 50) Description = forms.CharField (required = False, max_length = "254") photo = forms.ImageField () ** kwargs): reference = super (PhotoUploadView, itself) .get_context_data (** kwargs) reference ['user_info'] = self.request.user if 'upload_form' in kwargs: reference ['upload_form'] = kwargs ['upload_form '] And: reference [' upload_form '] = PhotoUploadForm () Album Reference =' ['Album' self.request.FILES print self.request.raw_post_data if self.request.method == "POST": form = PhotoUploadForm (self .request.POST, self.request. FI LES) if form.is_valid (): photo = photo () photo.title = form.cleaned_data ['title'] photo.summary = form.cleaned_data ['description'] photo.album = get_object_or_404 (album, id = kwargs ['Album_id']) photo.is_cover_photo = True path = self.generate_filename (self.request.FILES ['photo']. Name, self.request.user, kwargs ['album_id']) Destination = open (path, "wb +") to share in self. request. FILES ['photo']. Chunks (Destination): Destination. Close () photo.imagePath = Path photo.save () Return itself .render_to_response (self.get_context_data (Upload_form = form)
# forms.py class PhotoAddForm (forms.ModelForm): Class Meta: Model = Photo Def __INIT__ (self, album_ID, * args, ** quorz): self.album_id = Album_id super (PhotoAddForm, self) .__ init __ (* args, ** kwargs) def save (self, commit = True): photo = super (PhotoAddForm, self) .ave (commit = false) photo.a Lbum_id = self.album_id if commit: photo.save () Return photo # views.py class AddPhotoView (CreateView): form_class = Pho ToAddForm template_name = 'photos / add_photo.html' def get_success_url (self): reverse reverse ('photo_add_successful') def get_form_kwargs (self): kwargs = super (AddPhotoView, itself) .get_form_kwargs () kwargs.update ({'album_id': Self.kwargs ['album_id'],}) Return kwargs
Comments
Post a Comment