Sep 29, 2021 10:49 AM - edited Sep 29, 2021 11:23 AM
I need some assistance in determining the correct Hubl code for my scenario below. I am trying to grab part of a request query to use as the customer id in order to pull data from a HubDB table. The code was working correctly but I believe I broke it when adding an additonal parameter (assigned_dealer_v2=) to the requst query to be used in a hidden form field.
Example Request Query from the URL-
/dealer?assigned_dealer_v2=Indeco%20Sales&id=00010928-tx
My Hubl Code-
{% set dealer = '{{ request.query|split("=")|last|trim }}' %}
{% set dealerval = dealer|render %}
{% for row in hubdb_table_rows(5241455, 'customerid='~dealerval) %}
Using the example request query above, I am trying to pull the string after the last "=" sign - 00010928-tx
Any ideas on what I have set up wrong?
Thanks,
Terry McMillan
Solved! Go to Solution.
Sep 30, 2021 6:32 AM - edited Sep 30, 2021 7:42 AM
Your first line setting and assigning the dealer variable is invalid. Remove the quotation marks and remove the brackets. It should be:
{% set dealer = request.query|split("=")|last|trim %}
Sep 30, 2021 6:32 AM - edited Sep 30, 2021 7:42 AM
Your first line setting and assigning the dealer variable is invalid. Remove the quotation marks and remove the brackets. It should be:
{% set dealer = request.query|split("=")|last|trim %}
Sep 30, 2021 9:24 AM
@piersg That was the issue! Thanks so much for the suggestion. The issue is resolved and the data is displaying correctly now. Much appreciated.
Terry
Sep 29, 2021 9:25 PM
Update - I have still not been able to get it to work correctly. I am sure it's going to be something simple. I changed up the code a bit to make it easier to see what value is being rendered if I output it to the page.
New Code-
{% set dealer = '{{ request.query|split("=")|last|trim }}' %}
{% set dealerval = 'customerid='~dealer|render %}
{% for row in hubdb_table_rows(5241455, dealerval) %}
If I output {{ dealerval }} on the page, it shows a value of 'customerid=00010928-tx' which is correct and should filter the table to those rows that match.
If I manually input the info for dealerval as such:
{% set dealerval ='customerid=00010928-tx' %}
Everything works correctly and it pulls the appropriate info from the row and displays it on the page.
Any additional thoughts or suggestions would be greatly appreciated.
Thanks,
Terry
Sep 29, 2021 2:14 PM - edited Sep 29, 2021 2:16 PM
Hey @tmcmillan99
It might be easier to use request.query_dict. This variable breaks all querys into a dictionery thats much easier to use.
{% set the_query = request.query_dict %}
{% set dealer = the_query.id %}
With this method you can also handle situations when the query isn't present.
{% if !the_query.id %}
Somthing went wrong
{% endif %}
Sep 29, 2021 2:22 PM
Awesome @Kevin-C.
Thanks for the suggestion! I have not seen that variable before so I will definitely look into more.
Sep 29, 2021 12:10 PM
Hello @tmcmillan99 , can you output the dealer id with a {{dealerval}}
so that we can see that the correct value is pulled off the request query?
I used your exact code and it does indeed pull the "00010928-tx" value.
Maybe the issue is with the hubdb.
Sep 29, 2021 12:16 PM
@miljkovicmisa Thanks for the reply back and confirmation that the code is working as it should. I have not looked at the HubDB table yet. It was recently updated by the client so that will definitely be my next step.
Thanks for the assistance.
Terry