获取django Url模板标签来返回有用的东西:抛出反向错误 [英] getting django Url template tag to return something useful:throws reverse error

查看:82
本文介绍了获取django Url模板标签来返回有用的东西:抛出反向错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用Django {%url%} 模板标签,并保持失败。
视图在sitename / transfers / views.py中定义(其中transfer是django应用程序名称):

  def description_ListView(requesst,** kwargs):

template_name ='transfers / description.html'

o = get_list_or_404(Transfer,description = kwargs ['description'])
#print('o:',o)
context_object_name =transfer_name_list
返回render_to_response(template_name,{context_object_name:o,'description':kwargs ['description']})

(是的,我知道这段代码有点奇怪,努力使这个更通用,中间有这个令人讨厌的问题)



,并且URL被映射到transfer / urls.py

  url(r'^ description /(?P< description> [\w] +)/ $',
'transfers.views.description_ListView',
name =' description_url')

并在标签中:
{%urldescription_urlblabla%}
也尝试过:
{%url .Description_ListViewdescription =blabla%}



错误信息:

 异常类型:NoReverseMatch 
异常值:
反向''description_url'与参数'(u'blabla',)'和关键字参数'{}'未找到。

或者当我尝试使用作为somename 语法和调用如下:`{{somename}}。刚刚失败,没有任何东西。



我也尝试从视图导入Description_ListView并直接使用它,没有帮助。



此外,在各种SO问题中,遵循关于此主题的许多答案的建议,我更改为视图中的双引号,并恢复使用url名称而不是视图,但没有帮助。 p>

我很乐意为此提供任何帮助

解决方案

不要以为你需要报价。尝试:

  {%url description_urlblabla%} 

请参阅 https://docs.djangoproject.com/en/dev/topics/http/urls/#naming-url-patterns


trying to use the Django {%url %} template tag and keep failing. the view is defined in sitename/transfers/views.py (where transfers is the django app name):

def description_ListView(requesst,**kwargs):

    template_name = 'transfers/description.html'

    o = get_list_or_404(Transfer, description =kwargs['description'])
    #print ('o:',o)
    context_object_name = "transfer_name_list" 
    return render_to_response(template_name,{context_object_name:o,'description':kwargs['description']})

(yes, I DO know that this code is a little strange. working on making this more generic and caught in the middle with this annoying problem)

and the url is mapped in transfers/urls.py

url(r'^description/(?P<description>[\w ]+)/$',
                           'transfers.views.description_ListView',
                           name = 'description_url')

and in the tag: {% url "description_url" "blabla" %} also tried: {% url "transfers.views.Description_ListView" description = "blabla" %}

the error message:

Exception Type: NoReverseMatch
Exception Value:    
Reverse for '"description_url"' with arguments '(u'blabla',)' and keyword arguments '{}' not found.

or when i Tried using the as somename syntax and calling it like this: `{{somename}}. just failed silently and didn't produce anything.

where I also tried importing the Description_ListView from the views and using it directly, didn't help.

Also, following the advice of numerous answers on this subject in various SO questions I changed to double quotes around the view, and reverted to using the url name instead of view but neither helped.

I'll be glad for any help with this

解决方案

I don't think you need quotes. Try:

{% url description_url "blabla" %}

See https://docs.djangoproject.com/en/dev/topics/http/urls/#naming-url-patterns

这篇关于获取django Url模板标签来返回有用的东西:抛出反向错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆