But Problem is I don't get the images
model.py
----------------------
class tweet_upload(models.Model):
user = models.ForeignKey(User)
title = models.CharField(max_length = 100)
text = models.CharField(max_length=300)
created_date = models.DateTimeField(auto_now_add=True)
uploaded_image = models.ImageField(default = 'default.jpg', upload_to = 'static')
def __str__(self):
return f'{self.user.username} tweet_upload'
def save(self,*args, **kwargs):
super().save(*args, **kwargs)
img = Image.open(self.uploaded_image.path)
if img.height >300 or img.width>300:
output_size = (300,300)
img.thumbnail(output_size)
img.save(self.uploaded_image.path)
urls.py
----------------
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'$',views.post_view, name = "view"),
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL,document_root = settings.MEDIA_ROOT)
settings.py
---------------------
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR,'static')
forms.py
------------------
from django import forms
from .models import tweet_upload
class tweetform(forms.ModelForm):
class Meta:
model = tweet_upload
fields = ['uploaded_image','text','created_date','user','title']
admin.py
---------------------
from .models import tweet_upload
admin.site.register(tweet_upload)
views.py
--------------
from testapp.models import tweet_upload
from django.db.models import Q
# Create your views here.
def post_view(request):
tweet_list = tweet_upload.objects.all()
a = tweet_upload.objects.get(title = "Medicine (Django)").uploaded_image
b = a.url
return render(request,'post_view.html', {'tweet_list': tweet_list,'b':b})
post_view.html
_______________
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>tweet</title>
</head>
<body>
<div class="container" style="margin : 0px; padding : 0px;">
{% if tweet_list%}
{%if b%}
{% for i in tweet_list %}
<li>{{i.user}}</li>
<li>{{i.title}}</li>
<li>{{i.text}}</li>
<li>{{b}}</li> {%endfor%}
<img src="{{b}}" alt="not found">
{%else%}
<p>Niranjan is fell sorry for that </p>
{%endif%}
{%endif%}
</div>
</body>
</html>

Explain why I don't I get the image