2013-08-07 112 views
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 

回答

1

嗯,我沒有找到解決辦法,爲什麼它不工作,但我沒有找到一個解決方案,它的工作。希望這可以幫助其他人,或者可以向某人提供線索,爲什麼原始方法失敗。

我做了以下修改:

模板:

{% for fn in jpg_names %} 
    <img src="/tools/returnImage/{{ fn }}"> 
{% endfor %} 

URLS:

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/.*','returnImage',name='returnImage'),) 

瀏覽:

def returnImage(request): 
    path = "/opt/local/var/media/facstool/" 
    _, fn = os.path.split(request.META['PATH_INFO']) 
    f = open(str(path)+fn,'rb') 
    img = f.read() 
    f.close() 
    os.remove(str(path)+fn) 
    response = HttpResponse(img,mimetype='image/jpeg') 
    return response 

基本上,這增加了文件名img標籤,然後剝離在視圖中關閉文件名,在服務器上打開它,將其讀入一個變量,刪除原始文件並返回圖像。這是我想要的行爲,我只是不明白爲什麼OP不起作用。