1👍
First of all I think you are missing a forwardshals in your models.py
on line :
info_image = models.ImageField(upload_to='images/%Y%m/%d')
Unless it’s your intention, I think it should be like this:
info_image = models.ImageField(upload_to='images/%Y/%m/%d')
^
Next thing is that you are not providing the right url for href
attribute in the <a>
tag of your index.html
template.
{{forloop.counter}}.<a href="{{ item.info_image.url }}/">{{ item.info_text }}</a>
This line will point to the image itself. So you can use it example in the <image src="{{ item.info_image.url }}" />
but not in a link tag. So I guess this is what you were looking for.
To point to your detail
view of specific image you would want to ideally create get_absolute_url
method on your Info
model class.
Define a get_absolute_url() method to tell Django how to calculate the canonical URL for an object. To callers, this method should appear to return a string that can be used to refer to the object over HTTP.
For example:
# models.py
class Info(models.Model):
...
info_image = models.ImageField(upload_to='images/%Y%m/%d')
def get_absolute_url(self):
return reverse('detail',
args=[self.id])
Then you could use that in your template like this:
{{forloop.counter}}.<a href="{{ item.get_absolute_url }}">{{ item.info_text }}</a>
and display your image, wherever you want, using:
<image src="{{ item.info_image.url }}" />
0👍
Have you checked how the URL looks in the generated HTML code? E.g. does the URL look correct when the HTML is loaded, and when you click it, it starts repeating it?
- Trouble adding Django-Assets / Webassets directory to look for assets.py files
- How to create a User model object using Django REST framework?
- Unable to upload image using shell in django
- Cannot resolve keyword 'XXX' into field