djangodjango-formsdjango-templateshttp-status-code-404django-imagekit

can't upload image from database in django server


in my project i am trying to save image in database and next i want to get that image from database in my dashborad everything working well, imgs where saved in db but when i am trying to get url from db and asign it to img src="" chrome says that 404 (Not Found)

this is my model

class Id(models.Model):
    first_name = models.CharField(max_length=100, null=True)
    image = models.ImageField(default='defaut.jpg', upload_to='document_pics')

    def __str__(self):
        return self.first_nam

img savinig in documents_pics folder then i am trying to in views.py get data from db and send it to home.html

def home(request):
    context = {
        'posts': Id.objects.all(),
        'title': 'Home'
    }
    
    return render(request, 'blog/home.html', context)

home.html

{% extends 'blog/base.html' %}

{% block content %}

    {% for post in posts %}
        <article class="media content-section">
            <img src="{{ post.image }}  " width="200" height="200"
        </article>
    {% endfor %}
{% endblock content %}

and than i am getting 404 error


Solution

  • Try these path on your image jinja code in your html template

    {% extends 'blog/base.html' %}

    {% block content %}

    {% for post in posts %}
        <article class="media content-section">
            <img src="{{ post.image.url }}  " width="200" height="200"
        </article>
    {% endfor %}
    

    {% endblock content %