图片不会进入模板。
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
{% for question in question_list %}
<img src= "{{question.image}}">
class NewQuestion(models.Model):
category = models.ForeignKey(Category)
question_text = models.CharField(max_length = 200, unique = True, null = False)
image = models.ImageField(upload_to='images')
def __unicode__(self):
return self.question_text
答案 0 :(得分:0)
要使用Django的模板语言从ImageField渲染照片,需要以下语法:
{{ image.url }}
在你的情况下:
{% for question in question_list %}
<img src= "{{ question.image.url }}">
{% endfor %}
答案 1 :(得分:0)
试试这个:
{% for question in question_list %}
<img src= "{{question.image.url}}">
{% endfor %}
参考docs:
通过调用底层Storage类的url()方法来访问文件的相对URL的只读属性。
将此添加到 urls.py :
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
# ... the rest of your URLconf goes here ...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)