【发布时间】:2015-07-06 17:03:10
【问题描述】:
我是django 的新手。我想从模板传递日志文件名来查看日志文件名是Fuze-2015-04-28-08-07-34_28744226_30892878.log。当我点击 html 中的标签时,它给了我错误page not found
错误 NoReverseMatch 在 / 使用参数 '('Fuze-2015-04-27-00-42-55.log',)' 和关键字参数 '{}' 的 'parselog' 反向。尝试了 1 种模式:['parselog/(?P[-\w]+)/$']
模板渲染时出错
在模板 C:\fuzeLog\fuzeLog\templates\index.html 中,第 61 行出错 使用参数 '('Fuze-2015-04-27-00-42-55.log',)' 和关键字参数 '{}' 的 'parselog' 反向。尝试了 1 种模式:['parselog/(?P[-\w]+)/$']
61:{{ 日志文件 }}
index.html
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
<div class="row">
<div class="col-xs-6 col-sm-4" style='overflow:auto; width:400px; height:500px;'>
{% for logfile in logfiles %}
<a href="{% url 'parselog' logfile %}">{{ logfile }}</a>
<br>
{% endfor %}
</div>
</div>
</body>
</html>
urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
url(r'^$', 'fuzeLogApp.views.home', name='home'),
url(r'^parselog/(?P<logfile>[-\w]+)/$', 'fuzeLogApp.views.parselog', name='parselog'),
url(r'^admin/', include(admin.site.urls)),
)
view.py
def home(request):
try:
OSName = platform.system()
print OSName
if OSName == "Windows":
path="C:\\Users\\Ish_waR\\AppData\\Local\\FuzeBox\\Logs" # insert the path to your directory
Logfile_list =os.listdir(path)
else:
pass
except IOError as e:
print "I/O error({0}): {1}".format(e.errno, e.strerror)
return render_to_response('index.html',{'OSName':"I/O error({0}): {1}".format(e.errno, e.strerror)},context_instance=RequestContext(request))
return render_to_response('index.html',{'OSName':OSName ,'logfiles': Logfile_list},context_instance=RequestContext(request))
def parselog(request,logfile):
print logfile
【问题讨论】:
-
这是 url 中的关键字参数,而不是普通的 arg。你需要在url模板标签中做logfile=logfile。
-
{{ logfile }} 和上面一样?