Search code examples
pythondjangourlformview

Using multiples urls pointing to the same view, can't catch the variable from the url in the view


I'm using multiples urls pointing to the same View,'cause sometimes I send 2 values to the url and other just one, the problem is in the view when I try to catch the values I does't matter the name on the variable in the url, I use seguros and fac in the url as variables, but when a catch the values in the view it always catch it in the same variable fact,I need to catch them in diferente variables, 'cause in the template where I send them I need to do an if, but I can't. Can someone tell me why this is happening and what can I do so the value of seguro catch it in the variable seguro and the value of fac I catch it in fac. Here is my code.

Template.html

{% if facturas %}
    <a style="text-align: center" href="{% url 'seguimientoadministrativourls:crearseguimientoadministrativo' id=post.id_paciente.pk fac=facturas %}"><span class="glyphicon glyphicon-pushpin"></span> </a>
{% elif seguros %}
    <a style="text-align: center" href="{% url 'seguimientoadministrativourls:crearseguimientoadministrativo' id=post.id_paciente.pk seguros=seguros %}"><span class="glyphicon glyphicon-pushpin"></span>
</a>
{% else %}
    <a style="text-align: center" href="{% url 'seguimientoadministrativourls:crearseguimientoadministrativo' id=post.id_paciente.pk %}"><span class="glyphicon glyphicon-pushpin"></span>
</a>
{%endif%}

urls.py

url(r'^crearseguimientoadministrativo/(?P<id>\d+)$',
    permission_required(
        'seguimientoadministrativo.add_seguimientoadministrativobitacora',
        login_url='/sin_acceso')(CrearSeguimientoAdministrativo.as_view()),
    name='crearseguimientoadministrativo'),

url(r'^crearseguimientoadministrativo/(?P<id>\d+)/(?P<fac>\S+)$',
    permission_required(
        'seguimientoadministrativo.add_seguimientoadministrativobitacora',
        login_url='/sin_acceso')(CrearSeguimientoAdministrativo.as_view()),
    name='crearseguimientoadministrativo'),

url(r'^crearseguimientoadministrativo/(?P<id>\d+)/(?P<seguros>\S+)$',
    permission_required(
        'seguimientoadministrativo.add_seguimientoadministrativobitacora',
        login_url='/sin_acceso')(CrearSeguimientoAdministrativo.as_view()),
    name='crearseguimientoadministrativo'),

views.py

class CrearSeguimientoAdministrativo(ListView):
    template_name = 'crearseguimientoAdmin.html'
    model = SeguimientoAdministrativoBitacora
    paginate_by = 10

    def get_queryset(self, **kwargs):
        id = self.kwargs.get('id')
        query = DatosPaciente.objects.filter(pk=id)
        return query

    def get_context_data(self, **kwargs):
       context = super(
           CrearSeguimientoAdministrativo, self).get_context_data(**kwargs)
      context['seguimiento'] = SeguimientoAdministrativoBitacora.objects.filter(
        id_paciente=paciente)
      context['factura'] = self.kwargs.get('fac')
      context['seguro'] = self.kwargs.get('seguros')
      return context

Solution

  • How is the URL resolver supposed to distinguish between your URLs? They are both of the form "crearseguimientoadministrativo/numbers/string"; there is no way to tell which string represents a "fac" and which is a "seguros". You need to add some other way of distinguishing; perhaps with .../<id>/fac/<fac>/ and .../<id>/seguros/<seguros>/.