Add photo to profile page using thubnails?

  • 0
    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 are connected everywhere. Error in test output (Screen attached)



    Tell me what am I doing wrong?!?! Thank you very much in advance!!
    Django Isla Matthews, May 29, 2019

  • 3 Answers
  • 0
    # 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)
    Lila Curtis

  • 0
    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 ...
    Owen Logan

  • 0
    Yes, the image itself is present in the cache folder. The path to the file that created the thumbnails is written on the right side of the page code. It seems to me that I have a problem with the settings file. Because trying to just display the photo via & lt; img src = "{{person.image.url}}" / & gt; shows the same result.


    Part of my setings.py file:

    media_root: path to the storage folder
    MEDIA_ROOT = "/home/vladskliar/minisite"

    MEDIA_URL = "/"

    STATIC_URL = '/static/'


    the model says that upload photos to the photos folder:
    image = ImageField (upload_to = 'photos /')

    Please throw off a photo of an example of settings, views, template and model. That work.
    Anonymous

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!