我在URLField上保留了图片的网址,这样我就可以在我的主页模型上显示图像,而不是URL的字符串,我无法转换那个方向,“www。 google.es/images/car“在汽车的图像中?
models.py
def home (request):
photos = Photo.objects.all ()
html = '<ul>'
for photo in photos:
html + = '<li>' + photo.url + '</ li>'
html + = '</ ul>'
return HttpResponse (html)
views.py
photo.url
如何将图像<template>
转换为图像。
答案 0 :(得分:1)
您无法将字符串转换为图片,但您可以使用<img>
标记内的for photo in photos:
html + = '<li><img src="' + photo.url + '"></ li>'
属性中的网址
obj\Debug\main.o||In function `main':|
E:\project\test\main.cpp|15|undefined reference to `Stack<char>::Stack()'|
E:\project\test\main.cpp|23|undefined reference to `Stack<char>::shiftL()'|
E:\project\test\main.cpp|28|undefined reference to `Stack<char>::shiftR()'|
E:\project\test\main.cpp|33|undefined reference to `Stack<char>::Del()'|
E:\project\test\main.cpp|36|undefined reference to `Stack<char>::push(char)'|
||error: ld returned 1 exit status|
||=== Build failed: 6 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
请记住,您正在为网站生成HTML而不提供内容
答案 1 :(得分:-1)
尝试:
def home (request):
photos = Photo.objects.all ()
html = '<ul>'
for photo in photos:
html + = '<li><img src="{}"><li>'.format(photo.url)
html + = '</ ul>'
return HttpResponse (html)
也许更好,再次依赖于格式而不是字符串连接:
def home (request):
photos = Photo.objects.all ()
html = '<ul>{}</ul>'
html_photos = []
for photo in photos:
html_photos.append('<li><img src="{}"><li>'.format(photo.url))
html = html.format("\n".join(html_photos))
return HttpResponse (html)