J'ai un site django très simple mais j'ai du mal à afficher les images que je télécharge dans le panneau d'administration.Impossible d'obtenir des images à afficher dans un site django simple
mon settings.py
a ces constantes:
# Absolute path to the directory that holds media.
# Example: "/home/media/media.lawrence.com/"
MEDIA_ROOT = '/home/jeroen/programming/python/django/sitename/media'
# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash if there is a path component (optional in other cases).
# Examples: "http://media.lawrence.com", "http://example.com/media/"
MEDIA_URL = 'http://127.0.0.1:8000/media/'
# URL prefix for admin media -- CSS, JavaScript and images. Make sure to use a
# trailing slash.
# Examples: "http://foo.com/media/", "/media/".
ADMIN_MEDIA_PREFIX = '/media/'
mon urls.py
ressemble à ceci:
from django.conf.urls.defaults import *
from django.conf import settings
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Example:
# (r'^sitename/', include('sitename.foo.urls')),
# Uncomment the admin/doc line below and add 'django.contrib.admindocs'
# to INSTALLED_APPS to enable admin documentation:
(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
(r'^admin/', include(admin.site.urls)),
# http://docs.djangoproject.com/en/dev/howto/static-files/
# This method is inefficient and insecure.
# Do not use this in a production setting.
# Use this only for development.
(r'^media/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': settings.MEDIA_ROOT}),
)
et mon models.py
ressemble à ceci:
from django.db import models
class Truitje(models.Model):
titel = models.CharField(max_length=50)
beschrijving = models.TextField(max_length=500)
foto = models.ImageField(upload_to='truitjes')
def __unicode__(self):
return self.titel
Je peux télécharger avec succès images dans l'interface d'administration et ils obtiennent stor ed dans /home/jeroen/programming/python/django/sitename/media/truitjes
. Mais quand je vais à par exemple http://127.0.0.1:8000/media/truitjes/DSC00068.JPG
je reçois une erreur: Page not found: /media/truitjes/DSC00068.JPG
. Idem pour http://127.0.0.1:8000/media/truitjes
, et `` http://127.0.0.1:8000/media/gives
Autorisation refusée:/media/`.
Je voudrais ajouter à cette très bonne réponse que si vous avez des fichiers statiques qui sont globaux et sont dans un répertoire général "statique" je trouve après une lecture plus proche de les docs vous pouvez ajouter le répertoire dans le tuple STATICFILES_DIRS en utilisant le code suivant: os.path.join (os.path.realpath (os.path.dirname (__ fichier __)), 'static'), - ce qui serait utile pour CSS , par exemple. – Alastair