Django NoReverseMatch at / with arguments '('',)'

Django NoReverseMatch at / with arguments '('',)'

我有:

path('new/', views.new, name='new_without_arg'),
path('new/<str:arg>', views.new_with_arg, name='new_with_arg')

在我的 urls.py 中,当我在模板中有 'new_with_arg' 时:

<a href="{% url 'new_with_arg' subject %}">New Subject</a>

我得到:

'new_with_arg' 的反转,未找到参数“(”,)“。尝试了 1 种模式:['new/(?P[^/]+)$']

我的views.py是:


def new(request):
    return render(request, 'notes/new.html')

def new_with_arg(request, arg):

    if(arg == 'task'):
        return render(request, 'notes/new_task.html')

    elif(arg == 'room'):
        return render(request, 'notes/new_room.html')

    elif(arg == 'subject'):
        return render(request, 'notes/new_subject.html')
    
    else:
        return HttpResponseRedirect(reverse('new_without_arg'))

您是否将主题变量从 views.py 传递到 html 文件?

我明白了,必须是:

<a href="{% url 'new_with_arg' 'subject' %}">New Subject</a>