I have a very simple django site but I have trouble getting the images that I upload in the admin panel to show.
my settings.py
has these constants:
# 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/'
my urls.py
looks like this:
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}),
)
and my models.py
looks like this:
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
I can successfully upload pictures in the admin interface and they get stored in /home/jeroen/programming/python/django/sitename/media/truitjes
. But When I go to for example http://127.0.0.1:8000/media/truitjes/DSC00068.JPG
I get an error: Page not found: /media/truitjes/DSC00068.JPG
. Same for http://127.0.0.1:8000/media/truitjes
, and ``http://127.0.0.1:8000/media/gives
Permission denied: /media/`.
Change your media or admin URL / prefix to something different.
They cannot have both the same value. If they do, ADMIN_MEDIA_PREFIX
has precedence. That means if you try to access http://127.0.0.1:8000/media
, you are trying to access the admin media folder.
Either change the ADMIN_MEDIA_PREFIX
:
ADMIN_MEDIA_PREFIX = '/admin-media/'
or change the MEDIA_ROOT
:
# in settings.py
MEDIA_ROOT = '/home/jeroen/programming/python/django/ninikske/static'
MEDIA_URL = 'http://127.0.0.1:8000/static/'
# and in url.conf
(r'^static/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': settings.MEDIA_ROOT}),
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With