I am not able to get the image on my template from database. I had install pillow for supporting ImageField but its not working but It is successfully installed in my file proof.I am getting a broken image instead of uploaded image. Please help me out. note: I am not using virtual environment. Below are my codes. error: error pic image not found.
Views.py
def home(request):
dr_data = dr_reg.objects.all()
data ={
'dr_data':dr_data
}
return render(request, 'home.html',data)
models.py
class dr_reg(models.Model):
user = models.OneToOneField('User',on_delete=models.CASCADE, primary_key=True)
fname = models.CharField(max_length=50,blank=False)
lname = models.CharField(max_length=50,blank=False)
image = models.ImageField()
specialisation = models.CharField(max_length=100,blank=False)
qualificaton = models.CharField(max_length=100,blank=False)
phone = models.CharField(max_length=12,blank=False,unique=True)
gender = models.CharField(max_length=7,blank=False)
address = models.TextField(max_length=500,blank=False)
state = models.CharField(max_length=50,blank=False)
city = models.CharField(max_length=50,blank=False)
zip = models.CharField(max_length=50,blank=False)
email = models.EmailField(max_length=50,blank=False)
dUsername = models.CharField(max_length=100,blank=False,unique=True)
dPassword = models.CharField(max_length=100,blank=False)
def __str__(self):
return self.fname
home.html
{% extends 'base.html' %}
{% load static %}
<head>
<link rel="stylesheet" href="{% static 'style1.css' %}" type="text/css">
{% block title %}Home{% endblock title %}
</head>
{% block body %}
<div >
<div >
{% for n in dr_data %}
<div >
<div >
{% if n.image %}
<img src="{{ n.image.url }}" alt="img">
{% endif %}
<div >
<h5 >{{ n.fname }} {{n.lname}}</h5>
<p >{{ n.gender }}</p>
<p >Qualificaton: {{n.qualificaton}}</p>
<p >Specialisation: {{n.specialisation}}</p>
<p >Email: {{n.email}}</p>
<p >City: {{n.city}}</p>
<p >Contact: {{n.phone}}</p>
<p ><small >3 min ago:</small></p>
<a href=""
>
Book Appointment
</a>
</div>
</div>
</div>
{% endfor %}
</div>
</div>
{% endblock body %}
settings.py
MEDIA_ROOT = os.path.join(BASE_DIR, 'image')
MEDIA_URL = '/image/'
urls.py
from django.views import View
from . import views
from django.contrib import admin
from django.urls import path
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('', views.home, name="home"),
path('about', views.about, name="aboutus"),
path('contactus', views.contactus, name="contact"),
]
if settings.DEBUG:
urlpatterns = static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
I have add this settings.DEBUG in every url file.
CodePudding user response:
I guess the first problem would be that you are not specifying the upload_to parameter in your imageField so the image file doesn't get saved anywhere. It should look something like this
image = models.ImageField(upload_to="folder-in-media-root/")
Secondly, You need to install the pillow library to use the Django imageField
pip install pillow
Here's a link to the docs which you could take a look at ImageField: https://docs.djangoproject.com/en/4.1/ref/models/fields/#django.db.models.ImageField FileField: https://docs.djangoproject.com/en/4.1/ref/models/fields/#django.db.models.FileField