Нужно проверить есть ли какая нибудь картинка в базе данных или нет. Если нет , то отобразить надпись что картинки нет.
#settings.py
STATIC_URL = '/static/'
STATIC_ROOT=os.path.join(BASE_DIR,'static')
MEDIA_URL='/media/'
MEDIA_ROOT=os.path.join(BASE_DIR,'media')
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR,'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
#главная urls
from django.contrib import admin
from django.urls import path,include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('',include('images_app.urls'))
]
if settings.DEBUG:
urlpatterns +=static(settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT)
#images_app.urls
from django.urls import path
from .views import PageDetailView,PageCreateView
from . import views
urlpatterns = [
path('', views.index, name='home'),
# path('', PageHomeView.as_view(), name='home'),
path('new/', PageCreateView.as_view(), name='post_new'),
path('<int:pk>/', PageDetailView.as_view(), name='post_detail')
]
#images_app.views
from django.shortcuts import render,redirect
# Create your views here.
from django.views.generic import ListView,CreateView,DetailView
from .models import Post
def index(request):
posts=Post.objects.all()
return render(request, 'home.html', {'posts':posts})
class PageCreateView(CreateView):
model = Post
template_name = 'post_new.html'
fields = ('title','cover','image')
class PageDetailView(DetailView):
model = Post
template_name = 'post_detail.html'
fields=('title','cover','image')
#images_app.modls
from PIL import Image
from django.db import models
from django.urls import reverse
# Create your models here.
class Post(models.Model):
image=models.CharField(max_length=1024,)
title=models.TextField()
cover = models.ImageField(upload_to='images/')
def __str__(self):
return self.title
def save(self, *args, **kwargs):
super(Post, self).save(*args, **kwargs)
# Выбор картинки
img = Image.open(self.cover.path)
# Условие
if img.height > 300 or img.width > 300:
output_size = (300, 300)
img.thumbnail(output_size)
img.save(self.cover.path)
def get_absolute_url(self):
return reverse('post_detail', args=[str(self.id)])
#templates
#home.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
{%block content%}
<a href="/"> <h1>Списо изображений</h1> </a>
<a href="{% url 'post_new'%}"> <h2> Добавить изображения </h2> </a>
{% for post in posts %}
<a href="{{post.cover.url}}">
<h2>{{ post.title }}</h2>
<!-- <img src="{{ post.cover.url}}"> </a>-->
<!-- <h2>{{post.cover.url}} </h2>-->
{% endfor %}
{%endblock content%}
</body>
</html>
#post_detail.html
<code lang="html">
{%extends 'home.html'%}
{%block content%}
<p> Back to <a href="{% url 'home' %}"> All Posts </a></p>
<div class="post-entry">
<h2> {{object.title}} </h2>
<h2> <img src="{{object.image}}"></h2>
<h2> <img src="{{object.cover.url}}"></h2>
</div>
{%endblock content%}
</code>
#post_new.html
{% extends 'home.html' %}
{% block content %}
<h1>New article</h1>
<form action="" method="POST" enctype="multipart/form-data">
{% csrf_token%}
{{form.as_p}}
<button class="btn btn-outline-primary" type="submit">
<i class="fas fa-upload"></i> Upload</button>
</form>
{%endblock%}