在/ |获取错误NoReverseMatch

时间:2018-04-29 13:36:42

标签: python django

错误 - NoReverseMatch at / 反转' Detail_function'有参数'('',)'未找到。尝试了1种模式:['功能\ /(?P [0-9] +)\ / $']

我的代码 urls.py

urlpatterns = [
    path('',views.HomeFunction, name= 'home_function'),

    path('function/<int:pk>/',views.DetailFunction, 
name='Detail_function'),
]

models.py

class Profile(models.Model):
    name = models.CharField(max_length=100)
    gender = models.CharField(max_length=100)

    def __str__(self):
        return self.name

views.py

def HomeFunction(request):
    form = Profile.objects.all()
    context ={'form':form}
    return render (request,'home_function.html',context)

def DetailFunction(request,pk):
    form=get_object_or_404(Profile, pk=pk)
    return render(request,'detail_function.html',{'form':form})

模板 home_function.html

    {% for i in form %}
    <ul>
        <li><a href="">{{i.name}},{{i.gender}}</a> </li>

    </ul>
{% endfor %}

detail_function.html

<ul><li>{{form.name}}  |  {{form.gender}}</li></ul>
  

在我添加{%url&#39; Detail_Function&#39; home_function.html中的object.pk%},如下所示

{% for i in form %}
<ul>
    <li><a href="{% url 'Detail_function' object.pk %}">{{i.name}}, 
   {{i.gender}}</a> </li>

</ul>
{% endfor %}

在/处抛出NoReverseMatch 反转&#39; Detail_function&#39;有参数&#39;(&#39;&#39;,)&#39;未找到。尝试了1种模式:[&#39;功能\ /(?P [0-9] +)\ / $&#39;]

<a href="{% url 'Detail_function' object.pk %}">

显示错误

请指导我,提前致谢

1 个答案:

答案 0 :(得分:0)

对象不是表单的实例。要生成网址,你应该使用我

<a href="{% url 'Detail_function' i.pk %}">