我正在编写一个从数据库中获取文件名并输出相关图像的应用程序。但是,django模板似乎无法解析我的请求
这是django视图:
def nii2(request):
conn_str_file = os.path.join(os.path.dirname(os.path.abspath(__file__)),
'connection_string.txt')
connection_string = open(conn_str_file).read()
cursor = db.connect(connection_string).cursor()
sql = '''
SELECT img.[djangoFileLoc]
FROM [image_locations$] img,
[Metadata$] meta
WHERE img.[originalFileName] = meta.[originalFileName]
'''
if 'Machine' in request.GET:
sql += 'AND Machine = %r' % request.GET['Machine']
if 'Dir' in request.GET:
if request.GET['Dir'] != 'Both':
sql += ' AND Dir = %r' % request.GET['Dir']
sql += ' ORDER BY img.[originalFileName]'
djangoImages = []
sql += ';'
data = cursor.execute(sql)
for row in data:
djangoImages.append(row[0])
context = {"imageLoc": djangoImages[0:21]}
return render(request, 'template.html', context)
这是template.html:
{% extends "images.html" %}
{% load staticfiles %}
{% block images %}
{% for img in imageLoc %}
<a><img onmouseover="previewFunction('{% static '{{img}}' %}', 'Image One', 'Detailed info on Image one')"
src='{% static '{{img}}' %}' title ="Image 1"/></a>
{% endfor %}
{% endblock %}
这就是输出源的结果:
<a><img onmouseover="previewFunction('/static/%7B%7Bimg%7D%7D', 'Image One', 'Detailed info on Image one')"
src='/static/%7B%7Bimg%7D%7D' title ="Image 1"/></a>
<a><img onmouseover="previewFunction('/static/%7B%7Bimg%7D%7D', 'Image One', 'Detailed info on Image one')"
src='/static/%7B%7Bimg%7D%7D' title ="Image 1"/></a>
<a><img onmouseover="previewFunction('/static/%7B%7Bimg%7D%7D', 'Image One', 'Detailed info on Image one')"
src='/static/%7B%7Bimg%7D%7D' title ="Image 1"/></a>
<a><img onmouseover="previewFunction('/static/%7B%7Bimg%7D%7D', 'Image One', 'Detailed info on Image one')"
src='/static/%7B%7Bimg%7D%7D' title ="Image 1"/></a>
<a><img onmouseover="previewFunction('/static/%7B%7Bimg%7D%7D', 'Image One', 'Detailed info on Image one')"
src='/static/%7B%7Bimg%7D%7D' title ="Image 1"/></a>
<a><img onmouseover="previewFunction('/static/%7B%7Bimg%7D%7D', 'Image One', 'Detailed info on Image one')"
src='/static/%7B%7Bimg%7D%7D' title ="Image 1"/></a>
<a><img onmouseover="previewFunction('/static/%7B%7Bimg%7D%7D', 'Image One', 'Detailed info on Image one')"
src='/static/%7B%7Bimg%7D%7D' title ="Image 1"/></a>
<a><img onmouseover="previewFunction('/static/%7B%7Bimg%7D%7D', 'Image One', 'Detailed info on Image one')"
src='/static/%7B%7Bimg%7D%7D' title ="Image 1"/></a>
<a><img onmouseover="previewFunction('/static/%7B%7Bimg%7D%7D', 'Image One', 'Detailed info on Image one')"
src='/static/%7B%7Bimg%7D%7D' title ="Image 1"/></a>
<a><img onmouseover="previewFunction('/static/%7B%7Bimg%7D%7D', 'Image One', 'Detailed info on Image one')"
src='/static/%7B%7Bimg%7D%7D' title ="Image 1"/></a>
<a><img onmouseover="previewFunction('/static/%7B%7Bimg%7D%7D', 'Image One', 'Detailed info on Image one')"
src='/static/%7B%7Bimg%7D%7D' title ="Image 1"/></a>
<a><img onmouseover="previewFunction('/static/%7B%7Bimg%7D%7D', 'Image One', 'Detailed info on Image one')"
src='/static/%7B%7Bimg%7D%7D' title ="Image 1"/></a>
<a><img onmouseover="previewFunction('/static/%7B%7Bimg%7D%7D', 'Image One', 'Detailed info on Image one')"
src='/static/%7B%7Bimg%7D%7D' title ="Image 1"/></a>
<a><img onmouseover="previewFunction('/static/%7B%7Bimg%7D%7D', 'Image One', 'Detailed info on Image one')"
src='/static/%7B%7Bimg%7D%7D' title ="Image 1"/></a>
<a><img onmouseover="previewFunction('/static/%7B%7Bimg%7D%7D', 'Image One', 'Detailed info on Image one')"
src='/static/%7B%7Bimg%7D%7D' title ="Image 1"/></a>
<a><img onmouseover="previewFunction('/static/%7B%7Bimg%7D%7D', 'Image One', 'Detailed info on Image one')"
src='/static/%7B%7Bimg%7D%7D' title ="Image 1"/></a>
<a><img onmouseover="previewFunction('/static/%7B%7Bimg%7D%7D', 'Image One', 'Detailed info on Image one')"
src='/static/%7B%7Bimg%7D%7D' title ="Image 1"/></a>
<a><img onmouseover="previewFunction('/static/%7B%7Bimg%7D%7D', 'Image One', 'Detailed info on Image one')"
src='/static/%7B%7Bimg%7D%7D' title ="Image 1"/></a>
<a><img onmouseover="previewFunction('/static/%7B%7Bimg%7D%7D', 'Image One', 'Detailed info on Image one')"
src='/static/%7B%7Bimg%7D%7D' title ="Image 1"/></a>
<a><img onmouseover="previewFunction('/static/%7B%7Bimg%7D%7D', 'Image One', 'Detailed info on Image one')"
src='/static/%7B%7Bimg%7D%7D' title ="Image 1"/></a>
<a><img onmouseover="previewFunction('/static/%7B%7Bimg%7D%7D', 'Image One', 'Detailed info on Image one')"
src='/static/%7B%7Bimg%7D%7D' title ="Image 1"/></a>
因此,不是从列表中插入文件名,而是只读取变量。
有没有人对如何解决这个问题有任何想法?
答案 0 :(得分:2)
经过一周的思考,制动并在这里发布问题后,我发布后终于找到了解决方案。我刚删除变量周围的括号:
{% extends "images.html" %}
{% load staticfiles %}
{% block images %}
{% for img in imageLoc %}
<a><img onmouseover="previewFunction('{% static img %}', 'Image One', 'Detailed info on Image one')"
src='{% static img %}' title ="Image 1"/></a>
{% endfor %}
{% endblock %}
&#13;