【问题标题】:Reverse for 'evolucion_paciente' with arguments '(5,)' not found. 1 pattern(s) tried: ['evolucion_paciente/(?P<id>[0-9]+)/(?P<id_e>[0-9]+)$']未找到带有参数“(5,)”的“evolucion_paciente”的反向操作。尝试了 1 种模式:['evolucion_paciente/(?P<id>[0-9]+)/(?P<id_e>[0-9]+)$']
【发布时间】:2020-08-16 19:26:56
【问题描述】:

我创建了一个接受 3 个参数的视图,但我在主页中收到以下错误。找不到参数“(5,)”的“evolucion_paciente”的反向。尝试了 1 种模式:['evolucion_paciente/(?P[0-9]+)/(?P[0-9]+)$']

Project/views.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>

【问题讨论】:

    标签: django django-views django-urls django-url-reverse


    【解决方案1】:

    正如错误所说,您定义了一个 url 模式,如:

    evolucion_paciente/(<b>?P&lt;id&gt;[0-9]+</b>)/(<b>?P&lt;id_e&gt;[0-9]+</b>)$

    所以你需要传递两个参数,一个用于id,一个用于id_e。但是在你的{% url … %},你只通过了一个:

    {% url 'evolucion_paciente' <b>evolucion.id</b> %}

    你需要多传一个:

    &lt;a href="{% url 'evolucion_paciente' <i>value-for-id</i> evolucion.id %}" class="btn btn-warning"&gt;Ver&lt;/a&gt;

    您需要在value-for-id 中填写id 的值。大概是这样的:

    &lt;a href="{% url 'evolucion_paciente' <b>evolucion.paciente.id</b> evolucion.id %}" class="btn btn-warning"&gt;Ver&lt;/a&gt;

    【讨论】:

      猜你喜欢
      • 2017-11-05
      • 1970-01-01
      • 1970-01-01
      • 2023-03-28
      • 2021-04-24
      • 2022-01-13
      • 2023-03-18
      • 2022-08-05
      • 2021-12-12
      相关资源
      最近更新 更多