python - Image Not Displaying From The Path That is Manually Saved to Another Model in Django -
i have 2 models imagefield
. i using sorl-thumbnail
generate thumbnail. first model has form binded while second gets imagefield
populated first one, when saved.
models.py:
#the first model class newimage(models.model): thumbnail = models.imagefield(upload_to='photos') time_created = models.datetimefield() #second class users(models.model): name = models.charfield(max_length=100) address = models.charfield(max_length=200) thumbnail = models.imagefield(upload_to='photos') time_created = models.datetimefield()
forms.py:
class imageform(forms.modelform): class meta: model = newimage fields = {'thumbnail'}
views.py:
def saveimage(request): if request.method == 'post': user = users.objects.get(id = 1) imageform = imageform(request.post, request.files or none) if imageform.is_valid(): upload_image = imageform.save(commit=false) upload_image.time_created = timezone.now() upload_image.save() #save image path user uploaded it. user.thumbnail = upload_image.thumbnail user.save()
if pass these templates: image = newimage.objects.get(id = 1)
, user = users.objects.get(id = 1)
.
this displays image:
{% thumbnail image.thumbnail "200x100" crop="center" im %} <img src="..{{ im.url }}" width="{{ im.width }}" height="{{ im.height }}"> {% endthumbnail %}
but display square box:
{% thumbnail user.thumbnail "200x100" crop="center" im %} <img src="..{{ im.url }}" width="{{ im.width }}" height="{{ im.height }}"> {% endthumbnail %}
both images have link images sorl-thumbnail
created in cache folder.
why happening , how can user.thumbnail
displayed?
try this:
user.thumbnail = upload_image.thumbnail.name user.save()
one more thing:
instead of doing upload_image.time_created = timezone.now()
can change model include auto_now_add=true
automatically save time_created time created. this:
class newimage(models.model): thumbnail = models.imagefield(upload_to='photos') time_created = models.datetimefield(auto_now_add=true)
hope helps.
Comments
Post a Comment