Django: Problème pour afficher les produits
Bonjour, je souhaiterais afficher le nom et la photo du produit que j'ai crée sur l'admin. Bizarrement ce problème ne génére aucun message d'erreur, je vais vous montrer les différents dossier que j'ai crée pour le moment
model.py
class Product(models.Model):
name = models.CharField(max_length=128)
price = models.FloatField(default=0.0)
stock = models.IntegerField(default=0)
description = models.TextField(blank=True)
thumbnail = models.ImageField(upload_to='products',blank=True,null=True)
def __str__(self):
self.name
base.html (templates)
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<title>La boutique de maxime</title>
</head>
<body>
{% block content %}
{% endblock %}
</body>
index.html (templates)
from shop import settings
urlpatterns = [
path('', index, name='index'),
path('admin/', admin.site.urls),
] + static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)
{% extends 'base.html' %}
{% block content %}
<h1>La boutique de Maxime</h1>
{% for product in products %}
<h2>{{product.name}}</h2>
<img src="" alt="{{ product.name }}">
{% endfor %}
{% endblock %}
views.py
from django.shortcuts import render
from store.models import Product
def index(request):
products= Product.objects.all()
return render(request, 'store/index.html', context={'product': products})
urls.py
from shop import settings
urlpatterns = [
path('', index, name='index'),
path('admin/', admin.site.urls),
] + static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)
settings.py
from pathlib import Path
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/4.1/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-q%)^jd=34!losps1ug*$c5x4x^c=7lmk=&w5*dz#6-zt8$@y1$'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'store',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'shop.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [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',
],
},
},
]
WSGI_APPLICATION = 'shop.wsgi.application'
# Database
# https://docs.djangoproject.com/en/4.1/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
# Password validation
# https://docs.djangoproject.com/en/4.1/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/4.1/topics/i18n/
LANGUAGE_CODE = 'fr-fr'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.1/howto/static-files/
STATIC_URL = 'static/'
# Default primary key field type
# https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
MEDIA_URL= "/media/"
MEDIA_ROOT = BASE_DIR/ "media"
Quelqu'un pourrait m'aider à résoudre ce problème svp.
Windows / Chrome 103.0.0.0
A voir également:
- O2switch django
- O2switch - Accueil - Applis & Sites
- Organiser les données de manière organisée sur Django - Forum Algorithmes / Méthodes
1 réponse
Bonjour,
- Je n'ai jamais utilisé django, mais dans ta balise <img ...>, l'attribut src n'est pas spécifié. Or il devrait contenir l'URL vers ton image (voir ce lien).
- Assure-toi que cette URL est compatible avec ce qui est défini dans urlpatterns.
- Je t'invite aussi à regarder cette page de la documentation de Django.
Bonne chance