开发者

How to show an image in template uploaded from admin panel Django 1.3

I'm a newbie to django. I'm developing a project in django 1.3. Problem is I'm uploading a image from the admin panel

class About(models.Model):

    image = models.ImageField(upload_to='about')
    files = models.FileField(upload_to='about')

Here is my template tag

<img class="profile_pic" src="{{ about.image }}" />

My setting file is as below

MEDIA_ROOT = path("media/")
MEDIA_URL = '/media/'
STATIC_ROOT = ''
STATIC_URL = '/static/'
ADMIN_MEDIA_PREFIX = '/static/admin/'
STATICFILES_DIRS = (
    path('static/'),
)

I checked that image is uploaded to /media/about/image_name. Problem is it rendered in the template as "/about/imagename" but not shoing. When I manually go to that ima开发者_如何学编程ge url it showing a 404 error.


<img class="profile_pic" src="{{ about.image.url }}" />

UPDATE

Also in your urls.py:

if settings.DEBUG:
    urlpatterns += patterns('',
        (r'^' + settings.MEDIA_URL.lstrip('/'), 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT})


It worked!... After importing setting.MEDIA_ROOT in the urls.py file then I added a media rule so ITs working now..

BTW It also need

{{ about.image.url }} 

as @zsquare(thanks) said earlier Here is the media rule

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜