0
已經嘗試了許多以前發佈的相關解決方案,無效。我明顯錯過了一些東西。我正在使用Django 1.4。我有一個生成隨機數的jpeg文件的過程。我正在嘗試創建視圖/模板/模板標籤的組合,以便我動態顯示圖像。沒有模型參與。使用了來自Lois Di Qual的templatetag過濾器的想法Django build_absolute_uri拋出NoReverseMatch錯誤
settings.py文件具有正確的TEMPLATE_CONTEXT_PROCESSORS安裝。提前致謝。
這是我有:
模板:
{% load mytemplatetags %}
{% for fn in jpg_names %}
{{ fn|absolute_img_uri:request }}
{% endfor %}
模板標籤
from django import template
from django.template.loader import render_to_string
from django.core.urlresolvers import reverse
register = template.Library()
'''
Usage: {{ fn|absolute_media_url:request }}
'''
@register.filter
def absolute_img_uri(fn, request):
return request.build_absolute_uri(reverse("returnImage", kwargs={'fn':fn}))
查看:
def returnImage(request,**kwargs):
path = "/opt/local/var/media/facstool/"
fn = kwargs['fn']
img = open(str(path)+fn,'rb').read()
response = HttpResponse(img,mimetype='image/jpeg')
return response
的url:
import ri.tools.views as rv
urlpatterns = patterns('ri.tools.views',
url(r'^$', 'index', name="index"),
url(r'defineParameters','defineParameters',name='defineParameters'),
url(r'downloadcsv','downloadcsv',name='downloadcsv'),
url(r'downloadpdf','downloadpdf',name='downloadpdf'),
url(r'downloadZipFile','downloadZipFile',name='downloadZipFile'),
url(r'returnImage/(?P<fn>.*)',rv.returnImage,name='returnImage'),
)
* - 編輯 - *
回溯:
NoReverseMatch at /tools/defineParameters
Reverse for 'returnImage' with arguments '()' and keyword arguments '{'fn': u'Lep-_Clone12_Median__ss_293F_Median__ss_293F_H8_graph.jpg'}' not found.
Request Method: POST
Request URL: http://localhost:8000/tools/defineParameters
Django Version: 1.4
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'returnImage' with arguments '()' and keyword arguments '{'fn': u'Lep-_Clone12_Median__ss_293F_Median__ss_293F_H8_graph.jpg'}' not found.
Exception Location: /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/urlresolvers.py in _reverse_with_prefix, line 396
Python Executable: /opt/local/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python
Python Version: 2.7.2