Django-将命名URL与参数一起使用时出现NoReverseMatch错误

时间:2018-06-19 21:12:50

标签: django urlconf

我正在尝试使用带有参数的命名URL构建此链接:

http://127.0.0.1:8000/wakemeup/admin/list/colegio

参数是.../list/之后的字符串(在本例中为“ colegio”)。我在print()视图中有一个admin_list语句,它表明参数已正确提取。但是,当我尝试构建URL时:

<a href="{% url 'wakemeup:admin_list' list_type=colegio %}">Colegios</a>

我收到此错误:

NoReverseMatch at /wakemeup/admin/list/colegio
Reverse for 'admin_list' with keyword arguments '{'list_type': ''}' not found. 1 pattern(s) tried: ['wakemeup/admin/list/(?P<list_type>\\w+)$']
Request Method: GET
Request URL:    http://127.0.0.1:8000/wakemeup/admin/list/colegio
Django Version: 2.0.1
Exception Type: NoReverseMatch
Exception Value:    
Reverse for 'admin_list' with keyword arguments '{'list_type': ''}' not found. 1 pattern(s) tried: ['wakemeup/admin/list/(?P<list_type>\\w+)$']

views.py

def admin_list(request, list_type):
    print(list_type)
    return index(request)

urls.py

url(r'^admin/list/(?P<list_type>\w+)$', views.admin_list, name="admin_list"),

我也尝试使用未命名的参数,但这没用。

1 个答案:

答案 0 :(得分:0)

要在评论后确认答案,请记住将模板网址kwargs正确设置为字符串格式;

{% url 'wakemeup:admin_list' list_type='colegio' %}

要进一步阅读有关URL的文档,请参见此处; https://docs.djangoproject.com/en/2.0/topics/http/urls/