Retrieve database item using print vs return

56 views
Skip to first unread message

Maurice Waka

unread,
Nov 20, 2017, 10:41:55 PM11/20/17
to web2py-users
In my previous question: [web2py] How to get the last DB Field data :
I got some good response. But my issue still persists.
My code:

def view_searches():
    quizy = []
    form = SQLFORM(db.post).process()
    if request.vars.message:
        db.post.insert(message = request.vars.message)
    quizes = db(db.post.author == auth.user.id).select(db.post.ALL, orderby=~db.post.created_on, limitby=(0,1))
    for q in quizes:
        quizy.append(q.message)
    quiz= quizy[0]
    print quiz
    return dict(form=form, quiz=quiz)

Note the print quiz for illustration purpose.
Now note the print screen below:


Notice the last post is;'God is love'

  • If using print, i get the exact last posted item
  • if I use return on the html page, I get aowirhsgporhj......previous posted message, also shown on the terminal.
  • Note that the html page returns the random letter print;  aowirhsgporhj... instead of 'God is love'
  • I used print to illustrate the erros. If i remove print function, the error persists where i can't get the last posted item
My request is how do i resolve this?
Regards

Dave S

unread,
Nov 20, 2017, 11:15:55 PM11/20/17
to web2py-users


Perhaps you should have gotten a clean console screen before the run that you wanted to include in the post :-)
It's a little bit hard to see the relevant part of the screen.

Also, did you ever show us the view template code you are using?
 
Now, commenting on your code above, I would expect quizy[0] to be the only element in quizy, due to your limitby=(0,1).  I am not yet seeing why your page isn't showing the right result yet.  I suspect something in your view, which is why I asked about the template code.  But what is the blue bubble above the Send button?  It says "God is love" ... are you putting the result in the wrong place?

/dps

Maurice Waka

unread,
Nov 20, 2017, 11:44:35 PM11/20/17
to web...@googlegroups.com
Hi 
This is my code:
<html>
    <body>
        <div id="header">Chat Bot</div>
        <div id="messages">
        <a href='https://code.sololearn.com/Wm2n17OoSTDk/?ref=app'>Original</a>
        </div>
        <form id='user' onsubmit="return false;">
            <input type="text" id="msgbox" name='message' placeholder="Type a message..." />
            <button id="send"  onclick="ajax('{{=URL('view_searches')}}',['message'],'target');" >SEND</button>
        </form>
    </body>
</html>

<style>
    * {
       margin: 0px; 
       user-select: none;
       -webkit-user-select: none;
       -moz-user-select: none;
    }
    body {
        margin: 0px;
        height:400px;
        bottom:0;
    }
    #user {
        width: 100vw;
        position: fixed;
        left: 0px;
        bottom: 0px;
        display: block;
        background-color: #EEE;
        white-space: nowrap;
        height:105px;
    }
    #msgbox {
       width: calc(80% - 12px);
       min-height: 25px;
       max-height: 35px;
       padding: 5px;
       outline: none;
       border: solid 1px #AAA;
       display: inline-block;
       vertical-align: center;
       float: left;
       background-color: #FFF;
       border-radius: 25px;
       resize: none;
       margin: 0px;
       word-wrap:break-all;
    }
    #send {
       width: 20vw;
       height: 35px;
       display: inline-block
       outline: none;
       border: none;
       color: #FFF;
       background-color: #00F;
       float: left;
       border-radius: 25px;
       padding: 0px;
       cursor: pointer;
       margin: 0px;
    }
    #send:active {
       background-color: #00A; 
       outline: none;
    }
    #header {
       display: flex; 
       justify-content: left;
       align-items: center;
       width: calc(100% - 30px);
       height: 20px;
       padding: 15px;
       color: #FFF;
       font-size: 200%;
       font-weight: bolder;
       background-color: #00F;
       position: fixed;
       font-family: arial;
    }
    #messages {
       display: block;
       width: 100vw;
       height: calc(100% - 87px);
       background-color: #EEE;
       position: fixed;
       top: 50px;
       left: 0px;
       overflow: auto;
       overflow-x: hidden;
       overflow-y: auto;
    }
    .left {
       text-align: left; 
       /*
       display: block;
       */
    }
    .right {
       text-align: right; 
       /*
       display: block;
       */
    }
    .incoming {
       background-color: #FFF;
       color: #000;
       border: solid 1px #AAA;
    }
    .outgoing {
       background-color: #00F;
       color: #FFF;
    }
    .section {
       display: block; 
       width: calc(100% - 30px);
       padding-left: 15px;
       padding-right: 15px;
       margin-top: 7.5px;
       margin-bottom: 7.5px;
    }
    .message {
       display: inline-flex;
       justify-content: left;
       align-items: center;
       border-radius: 25px; 
       padding: 10px;
       font-size: 10pt;
    }
    input:first {
       color: #F00; 
    }
    .incoming:active {
       background-color: #EEE; 
    }
    .outgoing:active {
       background-color: #00A; 
    }
    * {
       outline: none; 
       tap-highlight: none;
       -webkit-tap-highlight: none;
       -webkit-tap-highlight-color: none;
       -moz-tap-highlight: none;
       -moz-tap-highlight-color: none;
       -khtml-tap-highlight: none;
       -khtml-tap-highlight-color: none;
    }
    a {
       display: block; 
       text-align: center;
    }
</style>


<script>
    $(function(){
        var commands = {
           "random":"var rand = (Math.floor(Math.random() * 10)); incoming(rand);",
           "commands":"var objstr = JSON.stringify(commands); objstr = JSON.stringify(commands, null, 4); incoming(objstr)"
        }
        var responseSys = {
                "hi":"Hello There!",
                "bye":"GoodBye!"
                };
                function cmd(name, action){
                   commands[name] = action;
                }
        function outgoing(text){
            var newMsg = "<div class='section right'><div class='message outgoing'>" + text + "</div></div>";
            $("#messages").append(newMsg);
        }
        function incoming(text){
         var newMsg = "<div class='section left'><div class='message incoming'>" + text + "</div></div>";
            $("#messages").append(newMsg);
            window.scrollTo(0, parseInt($("#messages").innerHeight))
        }
        $("#send").click(function(){
            $("#msgbox").trigger("blur")
            var text = $("#msgbox").val();
            if(text != null && text != ""){
            $("#msgbox").val("");
            text = text.replace(/</ig, "&lt;");
            text = text.replace(/>/ig, "&gt;");
            text = text.replace(/\n/ig, "<br />");
            outgoing(text);
            reply(text)
            }
            else{
               // Praise the Sun 
               incoming("Please, don't send empty messages.")
            }
        });
        $("#msgbox").keyup(function(e){
            if(e.which == 13){
               $("#send").trigger("click")
            }
            else{
               // Do Nothing 
            }
        });

        incoming("Hello!<br />This is a small ChatBot made by Andrew Grider. Feel Free to implement any commands or functions but please don't change this message! If you do add to the code however, then feel free to add your name to the contributors message!");
        incoming("Add responses using this JavaScript function, responses(your_message, my_reply) and add commands using this function, cmd(command_name, command_action)");
        incoming("Contributors:<br />Maz<br />The Coding Sloth")
        function responses(msg, response){
        msg = msg.toLowerCase();
            responseSys[msg] = response;
        }
        function reply(txt){
        txt = txt.toLowerCase();
        if(txt[0] == "r" && txt[1] == "e" && txt[2] == "s" && txt[3] == "p" && txt[4] == "o" && txt[5] == "n" && txt[6] == "s" && txt[7] == "e" && txt[8] == "s" && txt[9] == "("){
                try{
                    eval(txt);
                }
                catch(e){
                   incoming(e);
                }
            }
            else if(responseSys[txt] != undefined && responseSys[txt] != null && responseSys[txt] != "" ){
               incoming(responseSys[txt]);

            }
            else if(commands[txt] != null && commands[txt] != undefined && commands[txt] != ""){
               try{
                   try{
                       eval(commands[txt])
                   }
                   catch(e){
                       incoming("Error Executing")
                   }
               }
               catch(e){
                  incoming("Command not defined") 
               }
            }
            else if(txt[0] == "c" && txt[1] == "m" && txt[2] == "d"){
               try{
                  eval(txt) 
               }
               catch(e){
                  incoming(e) ;
               }
            }
            else setTimeout(function () {
                return incoming('Just a sec...');
                }, 3000);
            return setTimeout(function () {
                return incoming('{{=quiz}}');
                }, 4000);
        }
        responses("example", "My Response")
        responses("hello", "Hello World.. or.. person.")
    });
</script>

--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/dN9KfQULX54/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Maurice Waka

unread,
Nov 20, 2017, 11:50:51 PM11/20/17
to web...@googlegroups.com
HI this is my code:
{{for q in quizes:}}
{{quiz = q.message}}
{{pass}}

Dave S

unread,
Nov 21, 2017, 12:18:19 AM11/21/17
to web2py-users


On Monday, November 20, 2017 at 8:50:51 PM UTC-8, Maurice Waka wrote:
HI this is my code:

Do you really have a lot of stuff outside of the <html></html> tags?  Or are you showing us several separate files?  I'm not sure how to tie this all together.

/dps

 
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.

Dave S

unread,
Nov 21, 2017, 12:22:43 AM11/21/17
to web2py-users


On Monday, November 20, 2017 at 8:15:55 PM UTC-8, Dave S wrote:
[...]  But what is the blue bubble above the Send button?  It says "God is love" ... are you putting the result in the wrong place?

I see the sololearn  example page puts my input into blue bubbles that are right-justified, like the "God is love" bubble in your picture.

/dps
 

Maurice Waka

unread,
Nov 21, 2017, 12:49:41 AM11/21/17
to web...@googlegroups.com
That's the first input message I typed. The return message in the white bubble should also be the same as the one in the bubble blue. Am just testing to see that I pick the right input from my modules. 

--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/dN9KfQULX54/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+unsubscribe@googlegroups.com.

Dave S

unread,
Nov 21, 2017, 12:52:50 AM11/21/17
to web...@googlegroups.com

A mildly amusing example.  Somewhat off-topic, here's a clip of me interacting with it ... that is, a clip of the html obtained by the Inspect Element tool.  (Yeah, a screen shot probably would have been easier, but this is more in the proper spirit, or something.)

Enter code here...</div><div class="section right"><div class="message outgoing">Do you prefer questions?
</div>
</
div><div class="section left"><div class="message incoming">I haven't been taught how to respond to that yet.
</div>
</div><div class="section right"><div class="message outgoing">I can'
t respond to "no response"
</div>
</
div><div class="section left"><div class="message incoming">I haven't been taught how to respond to that yet.
</div>
</div>

[it -- the output --  seems to be missing my send of "commands" here] [edit -- no, it's just out of order]

<div class="section left"><div class="message incoming">{

    "random": "var rand = (Math.floor(Math.random() * 10)); incoming(rand);",
    "commands": "var objstr = JSON.stringify(commands); objstr = JSON.stringify(commands, null, 4); incoming(objstr)"
}
</div>
</div>
<div class="section right"><div class="message outgoing">random
</div>
</div>
<div class="section left"><div class="message incoming">6
</div>
</div>
<div class="section right"><div class="message outgoing">commands
</div>
</div>
<div class="section right"><div class="message outgoing">hi
</div>
</div>
<div class="section left"><div class="message incoming">Hello There!
</div>
</div>
<div class="section right"><div class="message outgoing">responses("Question authority", "I do not question myself")
</div>
</div>
<div class="section right"><div class="message outgoing">Question authority
</div>
</div>
<div class="section left"><div class="message incoming">i do not question myself
</div>
</div>

<div
 
class="section right"><div class=
"message
outgoing"
>responses("are you for real?", "I am as real as you can
imagine"
</div>
</div>
<div class="section left"><div class="message incoming">SyntaxError: missing ) after argument list
</div>
</div>

<div
 
class="section right"><div class=
"message
outgoing"
>responses("are you for real?", "I am as real as you can
imagine!")
</div>
</div>
<div class="section right"><div class="message outgoing">Are you for real?
</div>
</div>
<div class="section left"><div class="message incoming">i am as real as you can imagine!
</div>
</div>


/dps

Dave S

unread,
Nov 21, 2017, 12:59:37 AM11/21/17
to web2py-users


On Monday, November 20, 2017 at 9:49:41 PM UTC-8, Maurice Waka wrote:
That's the first input message I typed. The return message in the white bubble should also be the same as the one in the bubble blue. Am just testing to see that I pick the right input from my modules. 


In that view sample (the 4 lines of web2py '{{...}}' stuff, where does quizes come from?  In your controller, you are returning quiz=quiz.

/dps

 

Maurice Waka

unread,
Nov 21, 2017, 1:05:47 AM11/21/17
to web...@googlegroups.com
You mean a screen shot of before input, and after the input? 

--

Maurice Waka

unread,
Nov 21, 2017, 1:13:14 AM11/21/17
to web...@googlegroups.com
Let me start a new app and build from there. Then I'll let you know. There could be something I insert somewhere that I don't know. 
Regards 

Dave S

unread,
Nov 21, 2017, 1:21:29 AM11/21/17
to web2py-users


On Monday, November 20, 2017 at 10:05:47 PM UTC-8, Maurice Waka wrote:
You mean a screen shot of before input, and after the input? 

I mean the top lines of
<URL:https://groups.google.com/d/msg/web2py/dN9KfQULX54/4KwZhAqnCwAJ>
(posted at 8:50 my time (Los Angeles area, UTC-8) )
It's a for-loop as embedded in your view file.

/dps


On 21 Nov 2017 8:59 AM, "Dave S" <snide...@gmail.com> wrote:


On Monday, November 20, 2017 at 9:49:41 PM UTC-8, Maurice Waka wrote:
That's the first input message I typed. The return message in the white bubble should also be the same as the one in the bubble blue. Am just testing to see that I pick the right input from my modules. 


In that view sample (the 4 lines of web2py '{{...}}' stuff, where does quizes come from?  In your controller, you are returning quiz=quiz.

/dps

 
On 21 Nov 2017 8:22 AM, "Dave S" <snide...@gmail.com> wrote:


On Monday, November 20, 2017 at 8:15:55 PM UTC-8, Dave S wrote:
[...]  But what is the blue bubble above the Send button?  It says "God is love" ... are you putting the result in the wrong place?

I see the sololearn  example page puts my input into blue bubbles that are right-justified, like the "God is love" bubble in your picture.

/dps
 

--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/dN9KfQULX54/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.

Maurice Waka

unread,
Nov 23, 2017, 7:38:44 AM11/23/17
to web...@googlegroups.com
4 lines in the html file is a second different way I was trying to get the result, the same I also did in the controller to get the result:quiz.
I re-did the app and used the same code but unfortunately am still getting the same result.
How or where did you place your {{=quiz}} in the html?
could my code in the controller be the issue?
May I see how you did yours? 
Regards


To unsubscribe from this group and all its topics, send an email to web2py+unsubscribe@googlegroups.com.

Dave S

unread,
Nov 24, 2017, 11:40:13 PM11/24/17
to web2py-users


On Thursday, November 23, 2017 at 4:38:44 AM UTC-8, Maurice Waka wrote:
4 lines in the html file is a second different way I was trying to get the result, the same I also did in the controller to get the result:quiz.
I re-did the app and used the same code but unfortunately am still getting the same result.
How or where did you place your {{=quiz}} in the html?
could my code in the controller be the issue?
May I see how you did yours? 

I don't have an example.  I just played around in the sandbox you pointed me to.   I would put it in the view stub returned by the ajax.

Note that the sandbox is doing everything client-side, which makes persistent storage more difficult (for me, at least; I'm not up on the local storage routines).  To have things done server-side, I probably would start fresh with awareness of what the sandbox did, but not translating it's code.

/dps
 
Reply all
Reply to author
Forward
0 new messages