Try removing this line:

new_item.image = request.FILES['image’]

That should already be handled for you by the form.

> On May 12, 2016, at 6:30 PM, Jagdeep Sidhu <js.sidh...@gmail.com> wrote:
> 
> In this i am getting the message "Image added successfully" but it not going 
> in the database
> 
> #models.py
> class Image(models.Model):
>     user = models.ForeignKey(settings.AUTH_USER_MODEL, 
> related_name='images_created')
>     title = models.CharField(max_length=200)
>     slug = models.SlugField(max_length=200, blank=True)
>     #url = models.URLField()
>     image = models.ImageField(upload_to='images/%Y/%m/%d')
>     description = models.TextField(blank=True)
>     created = models.DateTimeField(auto_now_add=True, db_index=True)
>     users_like = models.ManyToManyField(settings.AUTH_USER_MODEL, 
> related_name='images_liked', blank=True)
> 
>     def __str__(self):
>         return self.title
> 
>     def save(self, *args, **kwargs):
>         if not self.slug:
>             self.slug = slugify(self.title)
>         super(Image, self).save(*args, **kwargs)
> 
> #forms.py
> 
> class ImageCreateForm(forms.ModelForm):
> 
>       class Meta:
>               model = Image
>               fields = ('title', 'description', 'image')
> 
>       def clean_url(self):
>               url = self.cleaned_data['url']
>               valid_extensions = ['jpg', 'jpeg']
>               extension = url.rsplit('.', 1)[1].lower()
>               if extension not in valid_extensions:
>                       raise forms.ValidationError('The given URL does not 
> match valid image extensions.')
>               return url
> 
> 
> #views.py
> 
> @login_required
> def upload_image(request):
>     form = ImageCreateForm()
>     if request.method == 'POST':
>         form = ImageCreateForm(data=request.POST, files=request.FILES, 
> instance=request.user)
>         if form.is_valid() and form.is_multipart():
>             cd = form.cleaned_data
>             new_item = form.save(commit=False)
>             print (new_item)
>             print (request.FILES)
>             new_item.user = request.user
>             new_item.image = request.FILES['image']
>             new_item.save()
>             print (new_item.image)
>             #save_file(request.FILES['image'])
>             return HttpResponse('Thanks for uploading the image')
>         else:
>             return HttpResponse('Invalid image')
>     else:
>             form = ImageCreateForm()
>     return render_to_response('images/Image_upload.html', {'form': form}, 
> RequestContext(request))
> 
> 
> 
> 
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to django-users+unsubscr...@googlegroups.com 
> <mailto:django-users+unsubscr...@googlegroups.com>.
> To post to this group, send email to django-users@googlegroups.com 
> <mailto:django-users@googlegroups.com>.
> Visit this group at https://groups.google.com/group/django-users 
> <https://groups.google.com/group/django-users>.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/django-users/42234e1e-8ec3-49da-96ad-ba0e06742b57%40googlegroups.com
>  
> <https://groups.google.com/d/msgid/django-users/42234e1e-8ec3-49da-96ad-ba0e06742b57%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout 
> <https://groups.google.com/d/optout>.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/578738E3-F49E-41D7-840E-69C93653CEDA%40consbio.org.
For more options, visit https://groups.google.com/d/optout.
  • Image Upload Jagdeep Sidhu
    • Re: Image Upload Nikolas Stevenson-Molnar

Reply via email to