在Django中显示图像

时间:2011-04-26 08:12:16

标签: python django django-models django-admin django-forms

尝试使用特定的文件夹来显示图像,但不起作用,告诉我我做错了什么,哪里有什么不够?所有图片都在C:\dip\todo文件夹中。使用runserver。

settings.py

MEDIA_ROOT = 'C:/dip/todo/' 
MEDIA_URL = 'http://127.0.0.1:8000/media/'
ADMIN_MEDIA_PREFIX = '/admin-media/'

admin.py

class StatAdmin( admin.ModelAdmin ): 
  list_display = ( 'title', 'image') 

models.py

class Stat( models.Model ): 
  title = models.CharField(max_length = 255,verbose_name = "Статистика" ) 
  image = models.ImageField(upload_to='media')

  def __unicode__( self ): 
    return u"%s" % self.title

  class Meta:
    verbose_name = 'Статистика' 
    verbose_name_plural = 'Диаграммы' 

urls.py

urlpatterns = patterns( '',
(r'^site_media/(?P<path>.*)$', 'django.views.static.serve',{'document_root': settings.MEDIA_ROOT}), ) 

base_site.html

{% extends "admin/base.html" %} 
{% load i18n %} 
{% block title %}{{ title }} | Реестр инплантатов{% endblock %}
{% block branding %} 
<h1 id="site-name">Реестр инплантатов</h1> 
{% endblock %} 

1 个答案:

答案 0 :(得分:2)

您的媒体网址说:

MEDIA_URL = 'http://127.0.0.1:8000/media/'

但在网址中你有:

(r'^site_media/(?P<path>.*)$', 'django.views.static.serve',{'document_root': settings.MEDIA_ROOT}), )

尝试将site_media更改为media,它应该可以正常工作