V
V
Vladislav Sklyar2015-05-31 21:32:38
Django
Vladislav Sklyar, 2015-05-31 21:32:38

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)

My view:
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)
        )

My template:
{% 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>

thumbnails is connected everywhere. Error during test output (Screen attached)
fdcc883890434905a54ebbc0e0c48fcd.png
Tell me what I'm doing wrong?!?! Thank you very much in advance!!

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
sim3x, 2015-06-03
@VladSkliar

# 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)

A
Andrey, 2015-06-03
@andrey_u

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 %}

https://sorl-thumbnail.readthedocs.org/en/latest/e...

V
Vladislav Sklyar, 2015-05-31
@VladSkliar

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/'

it is written in the model that to upload a photo to the photos folder:
Please skinte a photo of an example of settings, a view, a template and a model. Which are working.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question