I am unable to filter images which are tied to a particular post.
As you can see in the code below, I have a foriegnkey called post in my image model. I am able to upload lumtiple images from the admin page without issues. But when I call these image objects in my views.py as shown below, the page breaks into error.
In view.py if I use Image.objects.all() instead of Image.objects.get(blogpost=pk)
#models.py
class BlogPost(models.Model):
title = models.CharField(max_length=255)
body = models.TextField()
created_on = models.DateTimeField(auto_now_add=True)
last_modified = models.DateTimeField(auto_now=True)
categories = models.ManyToManyField(âCategoryâ, related_name=âpostsâ)
class Image(models.Model):
image = models.ImageField(upload_to=âimages/â)
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
object_id = models.PositiveIntegerField()
content_object = GenericForeignKey(âcontent_typeâ, âobject_idâ)
post = models.ForeignKey(âBlogPostâ, on_delete=models.CASCADE)
#blogdetail.html
{% for item in myimages_objs %}
{% endfor %}
What am I doing wrong? Please advise.
Instead of the Foreignkey for the âpostâ attribute inside the âImageâ model, should I try using the manytoone key? My idea is each of the images that are uploaded need to be tied to that post only.
Just at first glance, your code Image.objects.get(blogpost=pk) is not correct
You should be able to use myimages_objs = blogpost.image_set.all() instead
When you create a relationship with ForeignKey, Django will automatically set up a ârelated managerâ for the other side of the relationship using the modelname_set syntax. You might find this section of the Django documentation helpful: https://docs.djangoproject.com/en/3.0/ref/models/relations/
If that doesnât fix your issue, post the error message you are getting and I can try and help further.
When you say that there are no images displayed, are you saying that the HTML for the images isnât being rendered, or that the images themselves arenât showing up on the page?
Also, have you verified in the database that the ForeignKey field in Images is being populated correctly?
(Itâs possible that youâre dealing with a data issue here and not a template rendering issue.)
@KenWhitesell thanks for the response. The html is rendering fine. But images are not being displayed. I have two images uploaded from the admin page. I can see the images in the media folder after the sucessful upload.
What do you mean the âblog detail html page is rendering fineâ? Are you saying that the img tags are being rendered? If so, can you actually post the html showing that the src attributes are correct?
Can you please post the actual html being rendered? It looks like the src attribute isnât being properly populated, which would imply a data error, so you should confirm that the url attribute is right.
Hi @KenWhitesell I am not sure how to post the html being rendered. Above I have posted a screenshot of the html page. Is that what you are referring to?
I am not sure how to post the html being rendered. Above I have posted a screenshot of the html page. Is that what you are referring to?
How do I verify if the src atribute is or isnt being properly populated? Thanks.
You need to use tools in your browser to inspect the HTML. In e. g. Firefox you can go to the menu at the top right, click âWeb Developerâ in the dropdown menu, then âPage sourceâ. This should let you see the whole pageâs HTML. You can then share the HTML here, preferably formatted by putting ``` in the line directly preceding the HTML code as well as the line directly following the HTML code,
# So that the HTML is presented like this, making it easier to read.
You can also right-click an object, e. g. the area where the image should appear, in Firefox and select âInspect elementâ in the dropdown menu. That will lead to a subwindow popping up at the bottom of your browser. You should see an <img src=ââŚâ âŚ> tag somewhere there. What value is âsrcâ set to?
Hi @datalowe
thanks for the details.
When I tried a static image, the picture rendered fine without issues.
But, when I tried object.image.url style, as you can see below page source that there is no src link shown.
<div class="col-md-8 offset-md-2">
<h1>first post title</h1>
<small>
May 24, 2020 |
Categories:
<a href="/myblog/general/">
general
</a>
</small>
<br clear="all" />
<p>first post body</p>
<br clear="all" />
<img src="" >
<!-- <img src="/static/images/sto-pho-195123.jpg" alt="Django Photo Gallery" class="opaque">-->
I almost hate to ask this, but weâve been through many iterations to get to this point, and I canât be sure any more what the status is of the various components involved. Please (re)post the current models, views, template, and your MEDIA_ROOT settings.
Also, as a general rule, when Iâm trying to debug a template like this, I either change my template to just output the text, use the Django Debug Toolbar to help understand the context being rendered, or just use the shell to help me understand whatâs going on.
For examples:
Instead of rendering the urls as part of an image tag, I might change my template to be something like this:
<div class="divi">
{% if blogpost.image %}
<hr>
{% for item in blogpost.images %}
{{ item }}<br>
{{ item.image }}<br>
{{ item.image.url }}<br>
<hr>
{% endfor %}
{% endif %}
</div>
The whole idea here is just to give me more usable information - and itâs quicker/faster/easier than installing Django Debug Toolbar if itâs not already installed and configured.
blogpost = BlogPost.objects.get(pk=1)
images = blogpost.image_set.all()
for i in images:
print(i)
print(i.image)
print(i.image.url)
The idea behind all this is to follow the process from beginning to end to see what is failing. Once you know where it has stopped working, then you can figure out why it stopped working and fix it!
Like @KenWhitesell writes, itâs a bit hard to follow what is going on with your models, views etc. (e. g. in your description of the models, you seem to reference a âCategoryâ model, but itâs not described), and hence whatâs causing the behavior you see. Before writing my first reply I actually tried to recreate your project just to understand how everything was linked, and for my own practice. I donât understand it all myself but at least I got it to work. Iâve now uploaded the project as a github repo, maybe comparing what youâve done to it might help?
The two images that I had uploaded from the admin page of the blog are listed above using the django shell queryset sucessfully. This means that the images are correctly associated withe corresponding blogpost pk=3.
So far so good.
Now I copied the above statements into the views.py as shown below: