Answer the question
In order to leave comments, you need to log in
Add photo to profile page using thubnails?
Good afternoon!! Please tell me, I want to add an image to the page using sorl - thumbnails. Here is my code:
My prayer:
class Person(models.Model):
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
birthdate = models.DateField('Date of birth')
bio = models.CharField(max_length=255)
image = ImageField(upload_to='photos/', blank=True, null=True)
def info(request, person_id):
person = get_object_or_404(Person, id=person_id)
contact = get_object_or_404(Contact, person_id=person_id)
return render_to_response(
'index.html',
{'person': person, 'contact': contact},
context_instance=RequestContext(request)
)
{% load thumbnail %}
<ul>
<li> {{ person.first_name }}</li>
<li> {{ person.last_name }}</li>
<li> {{ person.bio }}</li>
<li> {{ person.birthdate }}</li>
{% thumbnail person.image "100x100" as im %}
<img src="{{ im.url }}" />
{% endthumbnail %}
<li> {{ contact.email }}</li>
<li> {{ contact.jabber }}</li>
<li> {{ contact.skype_id }}</li>
<li> {{ contact.other_contacts }}</li>
</ul>
Answer the question
In order to leave comments, you need to log in
# settings.py
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
STATIC_URL = '/static/'
# urls.py
if DEBUG:
urlpatterns += static(MEDIA_URL, document_root=MEDIA_ROOT)
If you haven't figured it out yet:
{% thumbnail person.image "100x100" crop="center" as im %}
<img src="{{ im.url }}" width="{{ im.width }}" height="{{ im.height }}">
{% endthumbnail %}
Yes, the image itself is present in the cache folder. On the right side of the page code is the path to the file that created the thumbnails. It seems to me that I have a problem with the settings file. Because the attempt, just display the photo through <img src="{{ person.image.url }}" />
showing the same result.
Part of my settings.py file:
media_root: path to storage folder
MEDIA_ROOT = "/home/vladskliar/minisite"
MEDIA_URL = "/"
STATIC_URL = '/static/'
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question