Django.url.exceptions.NoReverseMatch,尽管设置了所有参数

2024-05-05 13:28:48 发布

您现在位置:Python中文网/ 问答频道 /正文

因此,我认为我添加的所有内容都是正确的,包括url模式、视图和html页面,但我不确定哪里出了问题

path('hero/<int:hero_id>/', manage_hero, name="manage_hero"),
@login_required
def manage_hero(request, hero_id):
    context = {'hero_bar' : True}
    profile = UserProfile.objects.get(user=request.user)
    hero = get_object_or_404(Accskill, id=hero_id)
    if not profile.player.guid == hero.guid:
        return render(request, "dashboard/no_permission.html", context={'redirect' : reverse("dashboard:dashboard")})
    context['hero'] = hero
    context['form'] = ModifyHeroForm(hero=context['hero'])
    return render(request, "hero/hero.html", context=context)
<td><a href="{% url 'hero:manage_hero' hero_id=hero.ID %}" class="btn btn-outline-primary" role="button" aria-disabled="true">管理英雄</a></td>

最后,反向工作

reverse('hero:manage_hero', kwargs={'hero_id':4})
'/heroes/hero/4/'

错误是

django.urls.exceptions.NoReverseMatch: Reverse for 'manage_hero' with no arguments not found. 1 pattern(s) tried: ['heroes/hero/(?P<hero_id>[0-9]+)/$']