getting django Url template tag to return something useful:throws reverse error

╄→гoц情女王★ 提交于 2019-12-25 06:08:22

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/10807601/getting-django-url-template-tag-to-return-something-usefulthrows-reverse-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!