Trying to render images from database

64 views
Skip to first unread message

Michael Starr

unread,
Mar 3, 2023, 10:01:03 PM3/3/23
to Django users
It's not working.
class PetOwnerDetailView(DetailView):
    model = PetOwner
    context_object_name = "owner"
    template_name = "pet_owner_profile.html"
    def get_context_data(self, *args, **kwargs):
        context = super().get_context_data(**kwargs)
        context['pet_photos'] = {}
        pets = PetOwner.objects.filter(name = "pets")
        for pet in pets:
            context['pet_photos'][pet] = []
            for photo in pet.pet_photos:
                context['pet_photos'][pet] += photo
        # context['pet_photos'] = {pet1: [photo1, photo2, photo3], pet2: [photo1, photo2]}
        return context

{% extends "base.html" %}
{% block header %}
{% endblock %}
{% block content %}
    {{ owner.name }}
    {{ owner.age }}
    {{ owner.location }}
    {{ owner.profile_photo }}
    {% for pet in owner.pets.all %}
        {{ pet.name }}
        {{ pet.animaltype }}
        {{ pet.age }}
        <!-- context['pet_photos'] = {pet1: [photo1, photo2, photo3], pet2: [photo1, photo2]} -->
        {% for photo in pet_photos.pet.photos %}
            <img src = "{{ photo.photo.url }}">
        {% endfor %}
    {% endfor %}
{% endblock %}


Nothing shows up. I've tried a million and a half ways to iterate over the data structure I've created to store pets as keys and photos in a list as values in get_context_data for 'pet_photos' but nothing ever displays.

Then there's the question of like, are my directory structure and settings file correct.

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')

pet_memorial/static/ is the file path. I don't know if that's right or it needs to be in the project sub-directory. It's impossible to figure this out online. No one explains these simple simple things CLEARLY.

So frustrating.

Michael



Michael Starr

unread,
Mar 4, 2023, 1:08:52 AM3/4/23
to Django users
I think this solution
should work but for some reason python got messed up on my laptop when I went mobile at the coffee shop. The PYTHONPATH got deleted the .venv environment was deleted but still displayed and I'm getting a ton of system bugs trying to run python makemigrations or python migrate.

Frustrating. I am using the python repair tool in the python installer to repair python. Will let you know if the solution works after I get my environment back up and workin.g

Sandip Bhattacharya

unread,
Mar 4, 2023, 9:37:26 AM3/4/23
to django...@googlegroups.com
Are you sure this is the right query? This is looking for pet owners with the name “pets”.

pets = PetOwner.objects.filter(name = "pets”)

Perhaps, you mean:
pets = PetOwner.pets.all()

- Sandip
> --
> 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...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/9754727b-7cab-43b1-99ef-923d3f1fc2b2n%40googlegroups.com.


Michael Starr

unread,
Mar 4, 2023, 11:31:30 PM3/4/23
to Django users
Thank you Sandip. I missed that.

In any case I have revamped the code with some help from stack overflow here https://stackoverflow.com/questions/75632266/displaying-an-image-stored-in-a-django-database
However the image alt text instead of the image displays! So weird!

class PetOwnerDetailView(DetailView):
    model = PetOwner
    context_object_name = "owner"
    template_name = "pet_owner_profile.html"
    def get_context_data(self, *args, **kwargs):
        context = super().get_context_data(**kwargs)
        pets = Pet.objects.all()
        pet_data = {}
        for pet in pets:
            pet_data[pet] = PetPhoto.objects.filter(pets=pet)
        context['pet_data'] = pet_data
        return context

{% extends "base.html" %}
{% block header %}
{% endblock %}
{% block content %}
    {{ owner.name }}
    {{ owner.age }}
    {{ owner.location }}
    {{ owner.profile_photo }}
    {% for pet in owner.pets.all %}
        {{ pet.name }}
        {{ pet.animaltype }}
        {{ pet.age }}
        <!-- context['pet_photos'] = {pet1: [photo1, photo2, photo3], pet2: [photo1, photo2]} -->
        <h1>{{ pet.name }} Photos</h1>
        {% for pet, photos in pet_data.items %}
            <h2>{{ pet.name }}</h2>
            {% for photo in photos %}
                <img src="{{ photo.photo.url }}" alt="{{ photo.title }}">
            {% endfor %}
        {% endfor %}
    {% endfor %}
{% endblock %}

Screenshot 2023-03-04 153038.png

So close yet so far! Argh! lol

Mike

Michael Starr

unread,
Mar 7, 2023, 5:31:02 PM3/7/23
to Django users
Well, my privilege of posting "" on SO was revoked so I can't get my questions answered anymore. So I guess I just won't be a developer now. Or the alternative, reinvent all code and the internet.
The answer the dude gave me wasn't sufficient and he was like, "Accept the answer!" Devious prick. And then he was like "share your code with me!" And what he does is copy your code into his github account and claim he "helped" you.
Blah. What a loser.
So anyway, it still shows img alt text BUT now when I upload a photo into the pet_photo object, it appends a six digit ASCII text thingy to the filename, so the filename isn't actually correct even, anymore.
So many problems. Django is NOT the api for perfectionists with deadlines.
Mike

Michael Starr

unread,
Mar 7, 2023, 6:15:57 PM3/7/23
to Django users
this https://docs.djangoproject.com/en/4.1/topics/files/ seems useful, but isn't really for me.
Mike

Michael Starr

unread,
Mar 7, 2023, 6:20:05 PM3/7/23
to Django users
Now it thinks the filepath to the image is, e.g., /media/shade_in_red_O67Z3QC.jpg  (that last six digit ascii thing is what I was talking about). It adds it for somer eason, I have no idea why. And it's randomized. I dind't do anything and it just started. I tried using {% static %} and then it started, actually. So I guess static is cursed.

The real file path is /media/shade_in_red.jpg. It was correct before, but even then, it didn't display the image.

Michael

Michael Starr

unread,
Mar 7, 2023, 6:20:41 PM3/7/23
to Django users
I am having exquisitely poor luck on this google group with assistance. Most other posts seem to have people chipping in. I don't know why society doesn't like me.
But know that the feeling is mutual.
Mike

Red Plant

unread,
Mar 7, 2023, 9:58:12 PM3/7/23
to django...@googlegroups.com
What's showing on the browser console?

If you're using chrome, press F12 to open developer tools, then find the console tab.

Michael Starr

unread,
Mar 7, 2023, 10:11:38 PM3/7/23
to Django users
this.window.gBrowserInit is undefined ext-browser.js:1134
    get activeTab chrome://browser/content/parent/ext-browser.js:1134
    candidates chrome://extensions/content/parent/ext-tabs-base.js:2091
    next self-hosted:1743
    query chrome://extensions/content/parent/ext-tabs-base.js:2113
    next self-hosted:1743
    from self-hosted:516
    query chrome://browser/content/parent/ext-tabs.js:1000
    query self-hosted:1359
    result resource://gre/modules/ExtensionParent.jsm:1156
    withCallContextData resource://gre/modules/ExtensionParent.jsm:639
    result resource://gre/modules/ExtensionParent.jsm:1155
    withPendingBrowser resource://gre/modules/ExtensionParent.jsm:649
    result resource://gre/modules/ExtensionParent.jsm:1154
    callAndLog resource://gre/modules/ExtensionParent.jsm:1107
    recvAPICall resource://gre/modules/ExtensionParent.jsm:1153
    AsyncFunctionNext self-hosted:810

Michael Starr

unread,
Mar 7, 2023, 10:16:24 PM3/7/23
to Django users
I am using FireFox.

Red Plant

unread,
Mar 8, 2023, 12:08:19 AM3/8/23
to django...@googlegroups.com
Have you checked each img src?

Red Plant

unread,
Mar 8, 2023, 12:09:04 AM3/8/23
to django...@googlegroups.com
If not, check each img src using your browser's dev tools.

Michael Starr

unread,
Mar 10, 2023, 3:04:36 PM3/10/23
to Django users
I don't understand what you mean. There are no images. There's no img src except in code. I can't just right click on it because it's not there.

And I know the src of the image, because I'm the one who coded it. Also it spits it out when I print the coded url. This was all in a previous post in this thread.
Reply all
Reply to author
Forward
0 new messages