[英]django template doesn't load image uploaded in ImageField properly
i know this question have been brought up a lot of times,but i have tried a lot of different things,not working add.i belive i might have forget to add something silly in my code,just cant figure out what.below is the code of my heading model: 我知道这个问题已经提出很多次了,但是我尝试了很多不同的事情,无法正常工作。我相信我可能忘记在代码中添加一些愚蠢的东西,只是无法弄清楚是什么。下面是我的标题模型代码:
class Heading(models.Model):
category = models.ForeignKey(Category)
title = models.CharField(max_length=5000)
content = RichTextField()
image= models.ImageField(null=True,blank=True)
date = models.DateField(default=datetime.now())
time = models.TimeField(default=datetime.now())
slug = models.SlugField(unique=True, null=True, blank=True)
i have also added below codes in my projects settings.py 我还在我的项目settings.py中添加了以下代码
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
below is my urls.py 以下是我的urls.py
from django.conf.urls import include, url ,patterns
from django.contrib import admin
from django.conf import settings
urlpatterns = [
url(r'^tinymce/', include('tinymce.urls')),
url(r'^ckeditor/', include('ckeditor_uploader.urls')),
url(r'^grappelli/', include('grappelli.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^news/', include('news.urls')),
]
if settings.DEBUG:
urlpatterns += patterns(
'django.views.static',
(r'^media/(?P<path>.*)',
'serve',
{'document_root': settings.MEDIA_ROOT}), )
this is how my directory is set up 这是我的目录设置方式
below is my template code 下面是我的模板代码
{% extends "base.html" %}
{%block main_meat%}
{%if headings%}
{%for heading in headings%}
<h1><a href="/news/headline/{{heading.slug}}">{{heading.title}}</a></h1>
<br>
<img src='{{heading.image}}'>
{{heading.content|safe}}
{%endfor%}
{%endif%}
{%endblock%}
when i inspect element on a webbrowser,this is the path of image that i get 当我检查Web浏览器上的元素时,这就是我得到的图像的路径
<img src="./image_name.png">
subash is the name of my project and news is the name of the app.i know i have posted a long question with lots of codes,please if anyone can point out what i am missing. subash是我的项目的名称,news是应用程序的名称。我知道我发布了一个很长的问题,其中包含很多代码,请问有人可以指出我所缺少的内容。
In your template intead of: 在您的模板界面中:
<img src='{{heading.image}}'>
try: 尝试:
{% if heading.image %}
<img src='{{heading.image.url}}'>
{% endif %}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.