[英]Django Image won't come through on template from image directory
我試圖在我的Django模板中獲取一張圖片。 有人可以幫忙嗎?
這是我的模板代碼:
<img src={{myImage}}/>
這是我的查看代碼:
def base_book(request):
theBook = Book.objects.get(title__contains="django")
theAuthors = Book.objects.get(id=2)
myAuthors = theAuthors.authors.all()
myImage = "{{STATIC_URL}}two_scoops_django.png"
return render(request, 'base_book.html', {'book': theBook, 'author': myAuthors, 'myImage': myImage})
我將圖像文件“ two_scoops_django.png”保存在我的設置中文件路徑上的圖像目錄中,如下所示:
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'templatesDir'),
os.path.join(BASE_DIR, 'imageDir'),
)
我嘗試了幾種不同的組合嘗試使它通過,但尚未奏效。
Here is my URL pattern as well if it helps:
# DB - generated URL
url(r'^base_book/$', base_book),
先感謝您!
{{ STATIC_URL }}
內容僅在模板中有效。 您必須將模板更改為
<img src="{{ STATIC_URL }}{{myImage}}"/>
然后您查看代碼以
def base_book(request):
(...)
myImage = "two_scoops_django.png"
return render(request, 'base_book.html',
{'book': theBook, 'author': myAuthors, 'myImage': myImage}
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.