开发者

Cannot get images to display in simple django site

I have a very s开发者_JAVA百科imple 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/givesPermission 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}),


In Django 1.3 if you're just trying to display an image in a view during development follow these steps:

  1. Create a folder titled "static" in your app's directory
  2. Place your image in your static folder
  3. Make sure STATIC_URL in your settings.py is set to '/static/' - this should be default
  4. In your app/urls.py:

    from django.contrib.staticfiles.urls import staticfiles_urlpatterns
    #..rest of url.py config...
    urlpatterns += staticfiles_urlpatterns()
    
  5. In your template, wherever you want your picture to show up use this template tag:

    <img src="{{ STATIC_URL }}pic.png" />
    

Look at Serving Static Files in Development here: https://docs.djangoproject.com/en/1.3/howto/static-files/#staticfiles-in-templates

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜