我创建了一个接受3个参数的视图,但在homepage.Reverse中得到以下错误,'evolucion_paciente'的参数'(5,)'没有找到。尝试了1种模式。['evolucion_paciente(?P[0-9]+)(?P[0-9]+)$']。
Projectviews.py -- 我的一个观点
def VerEvoluciones(request, id):
if request.method == 'GET':
paciente = Paciente.objects.get(id= id)
evoluciones = Evolucion.objects.filter(paciente= id).order_by('-fechaEvolucion')
evolucionForm = EvolucionForm()
else:
return redirect('index')
return render(request, 'evoluciones.html', {'evolucionForm': evolucionForm, "Evoluciones": evoluciones, "Paciente": paciente})
另一种观点,也是我有问题的观点。
def VerEvolucion(request, id, id_e):
evolucionForm= None
evolucion= None
try:
if request.method == 'GET':
paciente = Paciente.objects.get(id= id)
evolucion = Evolucion.objects.filter(paciente= id).get(id= id_e)
evolucionForm = EvolucionForm(instance= evolucion)
else:
return redirect('index')
except ObjectDoesNotExist as e:
error = e
return render(request, 'evolucion.html', {'evolucionForm': evolucionForm,
'Evolucion': evolucion,
'Paciente': paciente,
'Ver': True})
在我的模板中,我需要将我从第一个视图重定向到第二个视图的链接。
<a href="{% url 'evolucion_paciente' evolucion.id %}" class="btn btn-warning">Ver</a>
正如错误所说,你定义了一个url模式,比如。
evolucion_paciente/(?P<id>[0-9]+)/(?P<id_e>[0-9]+)$
所以你需要通过 两种 参数,一个用于 id
和一个用于 id_e
. 但在你的 {% url … %}
你只通过了一个。
{% url 'evolucion_paciente' evolucion.id %}
你需要通过一个额外的。
<a href="{% url 'evolucion_paciente' value-for-id evolucion.id %}" class="btn btn-warning">Ver</a>
在这里你需要填写 value-for-id
价值为 id
. 可能是这样的
<a href="{% url 'evolucion_paciente' evolucion.paciente.id evolucion.id %}" class="btn btn-warning">Ver</a>