Reputation: 2689
I have a program that takes a users input in a form and assigns the user input to a variable session.dataQuery
to be used through the program. session.dataQuery
is passed to the next function resultsDisplay()
where it is processed and used to generate a set of results. In the corresponding view resultsDisplay.html
, the users results are displayed along with some alternative suggestions.
I want the alternative suggestions to be clickable links that will call the resultsDisplay
function and change session.dataQuery
to the clicked value, generating new results based on the revised query. I have no idea how to change the variable this way. Would really love some help with this. Hope the question makes sense, if you need clarification I will be happy to do so.
def index():
#This function gets session.dataQuery
def resultsDisplay():
#This function uses the value assigned to session.dataQuery to
#generate a set of results
resultsDisplay.html:
<!-- Displays results obtained from resultsDisplay() -->
<ul>
<li>result_1</li>
<li>result_2</li>
...
</ul>
<!-- Alternative suggestions here. To be clickable links to reload this page. The
value be sent to resultsDisplay() to become the new session.dataQuery variable and
be reprocessed -->
Upvotes: 0
Views: 2146
Reputation: 5374
So it sounds like clicking on the link will reload the page as if the user had filled out the form differently.
Maybe you should have the links point to resultsDisplay with some GET variables, include one that tells 'resultsDisplay' what new value dataQuery should get and another one that tells it not to try to process the form, but rather just overwrite the dataQuery and render the page.
Something like
<!-- Alternative suggestions -->
{{ =A(alt_sugg, _href=URL(vars={'suggest':True, 'dataquery':new_query_value}) ) }}
EDIT:
what exactly is that code there? My code at the minute is:
{{for suggestion in suggestions:}}
<ul><li>
<a href="resultsDisplay.html" onclick="session.vars={{=suggestion}}>
{{=suggestion}}
</a></li></ul>
{{pass}}
So in 'onclick' you have session.vars=
... is session a javascript variable? If not, what you're doing will not work. By the time web2py is rendering the view, you should not be altering web2py's session variable. It should be done in a controller (resultsDisplay).
You could do this (note in the text you typed, your missing a closing quote after {{=suggestion}}
):
{{for suggestion in suggestions:}}
<ul><li>
<a href="resultsDisplay.html?suggest=True&suggestion={{=suggestion}}">
{{=suggestion}}
</a></li></ul>
{{pass}}
You don't need to add onclick events to <a>
tags since they already respond to clicks. By adding the suggestion to the URL, you can set the session variable if you like, or use the get variable (request.vars.suggestion
) directly.
Does that make sense?
fyi, my initial code just spits out essentially the same as what I just suggested.
This:
{{=A(suggestion, _href=URL(vars=dict(suggest=True, suggestion=suggestion))) }}
Produces the same markup (essentially) as this:
<a href="resultsDisplay.html?suggest=True&suggestion={{=suggestion}}">
{{=suggestion}}
</a>
EDIT:
So the code I have provided is only the view. One a 'suggestion' is clicked, the resultsDisplay() will execute again. That is where you set session.dataQuery:
def resultsDisplay():
if request.vars.suggest == True:
session.dataQuery = request.vars.suggestion
else:
#This BRANCH of the function uses the value assigned to session.dataQuery to
#generate a set of results.
Upvotes: 1