如何从管理面板Django 1.3上传的模板中显示图像 [英] How to show an image in template uploaded from admin panel Django 1.3
问题描述
class关于(models.Model):
image = models.ImageField(upload_to ='about')
files = models.FileField(upload_to ='about')
这是我的模板标签
< img class =profile_picsrc ={ {about.image}}/>
我的设置文件如下
MEDIA_ROOT = path(media /)
MEDIA_URL ='/ media /'
STATIC_ROOT =''
STATIC_URL ='/ static /'
ADMIN_MEDIA_PREFIX ='/ static / admin /'
STATICFILES_DIRS =(
path('static /'),
)
我检查了该图像是否已上传到/ media / about / image_name。问题是它在模板中渲染为/ about / imagename,但不能播放。当我手动去图像URL时,显示404错误。
它工作!...导入setting.MEDIA_ROOT后在urls.py文件中,然后我添加了一个媒体规则,所以IT现在工作..
BTW还需要
{{about.image.url}}
as前面说过的@zsquare(感谢)这里是媒体规则
(r'^ media /(?P< path> )$','django.views.static.serve',{'document_root':MEDIA_ROOT}),
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 image url it showing a 404 error.
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}),
这篇关于如何从管理面板Django 1.3上传的模板中显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!