Add thumbnails on search results

This commit is contained in:
Claude Paroz 2018-10-14 19:59:04 +02:00
parent f45b5d842b
commit ea6bb20ab3
4 changed files with 36 additions and 4 deletions

View file

@ -17,6 +17,7 @@ INSTALLED_APPS = [
'django.contrib.sessions', 'django.contrib.sessions',
'django.contrib.messages', 'django.contrib.messages',
'django.contrib.staticfiles', 'django.contrib.staticfiles',
'easy_thumbnails',
'recette', 'recette',
] ]
@ -75,4 +76,10 @@ STATIC_ROOT = os.path.join(BASE_DIR, 'static')
MEDIA_URL = '/media/' MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media') MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
THUMBNAIL_ALIASES = {
'': {
'thumbnail': {'size': (120, 120), 'crop': True},
},
}
from .local_settings import * from .local_settings import *

View file

@ -1,5 +1,7 @@
from django.conf import settings
from django.contrib import admin from django.contrib import admin
from django.urls import path from django.urls import path
from django.views.static import serve
from recette.views import home, recette from recette.views import home, recette
@ -9,3 +11,10 @@ urlpatterns = [
path('', home, name='home'), path('', home, name='home'),
path('recette/<int:pk>/', recette, name='recette'), path('recette/<int:pk>/', recette, name='recette'),
] ]
if settings.DEBUG:
urlpatterns += [
path('media/<path:path>', serve, {
'document_root': settings.MEDIA_ROOT,
}),
]

View file

@ -1,2 +1,3 @@
django>=2.0 django>=2.0
pillow pillow
easy-thumbnails==2.5

View file

@ -1,19 +1,34 @@
{% extends 'base.html' %} {% extends 'base.html' %}
{% load thumbnail %}
{% block extrahead %}
<style>
ul.results { list-style-type: none; padding: 0; }
ul.results li { margin: 0; padding: 2px; }
ul.results li, ul.results li * { vertical-align: middle; }
ul.results li:nth-child(odd) { background-color: #eee; }
input#id_text { padding: 10px 0; }
form#search-form p { display: inline-block; margin-right: 1em; }
</style>
{% endblock %}
{% block content %} {% block content %}
<h1>Recettes</h1> <h1>Recettes</h1>
<form method="POST">{% csrf_token %} <form id="search-form" method="POST">{% csrf_token %}
{{ form.as_p }} {{ form.as_p }}
<button>Rechercher</button> <div><button>Rechercher</button></div>
</form> </form>
{% if num_results is not None %} {% if num_results is not None %}
<div class="results">{{ num_results }} résultat(s)</div> <div class="results">{{ num_results }} résultat(s)</div>
{% endif %} {% endif %}
<ul> <ul class="results">
{% for recette in recettes %} {% for recette in recettes %}
<li><a href="{{ recette.get_absolute_url }}">{{ recette.nom }}</a></li> <li>
<a href="{{ recette.get_absolute_url }}">{{ recette.nom }}</a>
<img src="{{ recette.photo|thumbnail_url:'thumbnail' }}">
</li>
{% endfor %} {% endfor %}
</ul> </ul>
{% endblock %} {% endblock %}