【问题标题】:Pass value(words symbol and number) from template to url.py in django将值(单词符号和数字)从模板传递到 django 中的 url.py
【发布时间】: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 }} 和上面一样?

标签: regex django


【解决方案1】:

在 index.html 中使用 url template tag,并将 logfile 变量作为参数传递

{% for logfile in logfiles %}
<a href="{% url 'parselog' logfile %}">{{ logfile }}</a>
<br>
{% endfor %}

在 urls.py 中,你需要允许 '.'在正则表达式中:

url(r'^parselog/(?P<logfile>[\w.-]+)/$', 'fuzeLogApp.views.parselog', name='parselog'),

Common Regular Expressions for Django URLs

【讨论】:

  • 当我用你给定的代码 NoReverseMatch at / Reverse 替换我的 index.html 代码时出现以下错误'{}' 未找到。尝试了 1 种模式:['parselog/(?P[-\\w]+)/$']
  • 你能发布完整的 urls.py 吗?看起来您有一个以 url(r'^parselog/ 开头的 url,您的代码出于某种原因试图对其进行模式匹配。
  • 请将网址作为编辑添加到您的问题中,以便于查看。谢谢。
  • 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[-\w]+)/$', 'fuzeLogApp .views.parselog', name='parselog'), # url(r'^blog/', include('blog.urls')), url(r'^admin/', include(admin.site.urls) ), )
  • 看起来正则表达式没有正确捕捉到参数。可能是因为你在[-\\w] 中有一个额外的斜线。
猜你喜欢
  • 2015-11-13
  • 2021-02-03
  • 2015-07-07
  • 2018-02-10
  • 2020-06-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-19
相关资源
最近更新 更多