繁体   English   中英

Django网址错误“ / 2 / ipd /为NoReverseMatch”

[英]Django url error“NoReverseMatch at /2/ipd/”

我有创建Ipd的表格,并且Ipd模型是使用具有一对多关系的患者模型创建的,并且我已经有一个表,其中的患者列表位于url中。 我正在尝试创建使用表单创建的所有Ipd的列表,我试图在提交Ipd表单后将表单页面重定向到Idp列表,但是以错误“ / 1 / ipd /处的NoReverseMatch”结束,

我要清除的一件事是,每个Ipd具有唯一的ID,并且Ipd是由具有一对多关系的患者创建的,也具有另一个唯一ID,错误中的数字是患者ID

views.py

@login_required
def ipd(request, patient_id):


    object = get_object_or_404(Patient,pk=patient_id)
    if request.method == "POST":
        formtwo = IpdForm(request.POST)
        if formtwo.is_valid() :
            instance = formtwo.save(commit=False) 
            instance.save()
            return HttpResponseRedirect(reverse('ipd_list', args=[patient_id]))
        else:
            return HttpResponse(formtwo.errors) 
    else:
        formtwo = IpdForm()

    return render(request, 'newipd.html', {'object':object, 'form2': formtwo})

@login_required
def ipd_list(request):

    ipdlist = Ipd.objects.all()
    return render(request, 'Ipdlist.html', {'ipd': ipdlist })

urls.py

re_path(r'^$', my_patient.index, name='home'),      <-- patient list-->
re_path(r'^(?P<patient_id>\d+)/ipd/$', my_patient.ipd, name='ipd'),
path(r'^ipdlist/', my_patient.ipd_list,name='ipdlist' ),

模板


<ul>
<li><a href="{% url 'ipdlist' %}" ><span class="title">Indoor Patient Department</span></a></li>
</ul>

########
in ipdform

<form class="col s12" role="form" action="{% url 'ipd_list' 'patient_id' %}" method="post"  enctype="multipart/form-data"> {% csrf_token %}



由于要返回到ipd_list,因此必须删除args=[patiend_id]

return HttpResponseRedirect(reverse('ipd_list'))

您正在尝试使用参数将用户重定向到ipdlist网址。 由于这是一个列表方法,因此您不应该这样做。 您需要更改:

@login_required
def ipd(request, patient_id):


    object = get_object_or_404(Patient,pk=patient_id)
    if request.method == "POST":
        formtwo = IpdForm(request.POST)
        if formtwo.is_valid() :
            instance = formtwo.save(commit=False) 
            instance.save()
            return HttpResponseRedirect(reverse('ipd_list'))
        else:
            return HttpResponse(formtwo.errors) 
    else:
        formtwo = IpdForm()

    return render(request, 'newipd.html', {'object':object, 'form2': formtwo})

@login_required
def ipd_list(request):

    ipdlist = Ipd.objects.all()
    return render(request, 'Ipdlist.html', {'ipd': ipdlist })

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM