flash message gets lost

85 views
Skip to first unread message

Yebach

unread,
May 7, 2017, 5:25:17 AM5/7/17
to web2py-users
Hello

After redirecting user after registriation the flash message is not shown. if I print session the message gets lost --> flash is none

I inserted print session in my user function in default.py controller and in index function

This is the print result

<Storage {'flash': 'Thank you for registering. Please check your e-mail account', '_formkey[register]': ['cd76b587-db09-46fa-a05e-b31c2f71d4c4', 'ad7148b5-3306-455b-9c2d-65e6c91e9856', '069640cf-0c68-416c-b091-8e8efa337e24', 'd89158de-86a5-4e23-9a07-43ba042a09ac', 'fa1a99ba-8d97-49c2-ad0d-ffe7ba66e412', '5cfb4a89-8284-4879-9b1d-9bc923350771', '6b55ba7d-fa6f-4709-a8ca-0ec30e4f4455', 'c983f92b-10d6-47d7-b02f-a5bc7a4419b4', '7ff1a289-f9b4-47b6-92a2-da93fcdeb8ca', 'c298cda9-447b-47e4-b9b5-7b1a6f85645d'], '_formkey[login]': ['dc014554-b3a9-4ab7-8a4e-21600922d485', '4afe46f2-77a8-4e74-abc9-1d2d512a1d56', '3fa4d78a-4113-419f-ae90-5a39d3bac9f9', '171e7853-0f31-4e66-b8ef-1c8c665610b5', '86b76af0-3f51-4f47-95e6-903a117db2cb', 'f117d59d-7e38-4e90-a70c-fb9f507b3681', '756458fd-1f13-4aab-9f71-fae5322199c5', 'e5949f3d-3094-45f2-94ba-ff4c9ea27d08', '8abea09c-9493-499b-bf50-5487262a9a03', '467f8079-508a-4c83-bbf1-49ba86ade0f1'], '_formkey[auth_user/create]': ['5cf42aee-c3b1-4f2b-a4b5-6027ec55c7c3', '6a58a734-dc76-43b0-84df-fc35444a6364', '0cc70146-e946-411c-9ced-4552098a07c8', 'b189dc4c-d789-463b-a223-ce22bb880828', '4ebe96ab-1b25-4bcf-98bd-285f51b28168', 'd5f15dea-51c4-446e-a66e-f3aaccc3937b', 'a13cf1fe-a994-44e0-b03c-c5769f0d0d92', '7297c942-159e-491f-9a63-fc10420c1ebb', 'dbe6e93c-28e0-4b54-b4d5-3a0f4083059d', '199b91db-8921-42a2-a741-1dd323c09600']}>

session index  <Storage {'flash': None, '_formkey[register]': ['cd76b587-db09-46fa-a05e-b31c2f71d4c4', 'ad7148b5-3306-455b-9c2d-65e6c91e9856', '069640cf-0c68-416c-b091-8e8efa337e24', 'd89158de-86a5-4e23-9a07-43ba042a09ac', 'fa1a99ba-8d97-49c2-ad0d-ffe7ba66e412', '5cfb4a89-8284-4879-9b1d-9bc923350771', '6b55ba7d-fa6f-4709-a8ca-0ec30e4f4455', 'c983f92b-10d6-47d7-b02f-a5bc7a4419b4', '7ff1a289-f9b4-47b6-92a2-da93fcdeb8ca', 'c298cda9-447b-47e4-b9b5-7b1a6f85645d'], '_formkey[login]': ['dc014554-b3a9-4ab7-8a4e-21600922d485', '4afe46f2-77a8-4e74-abc9-1d2d512a1d56', '3fa4d78a-4113-419f-ae90-5a39d3bac9f9', '171e7853-0f31-4e66-b8ef-1c8c665610b5', '86b76af0-3f51-4f47-95e6-903a117db2cb', 'f117d59d-7e38-4e90-a70c-fb9f507b3681', '756458fd-1f13-4aab-9f71-fae5322199c5', 'e5949f3d-3094-45f2-94ba-ff4c9ea27d08', '8abea09c-9493-499b-bf50-5487262a9a03', '467f8079-508a-4c83-bbf1-49ba86ade0f1'], '_formkey[auth_user/create]': ['5cf42aee-c3b1-4f2b-a4b5-6027ec55c7c3', '6a58a734-dc76-43b0-84df-fc35444a6364', '0cc70146-e946-411c-9ced-4552098a07c8', 'b189dc4c-d789-463b-a223-ce22bb880828', '4ebe96ab-1b25-4bcf-98bd-285f51b28168', 'd5f15dea-51c4-446e-a66e-f3aaccc3937b', 'a13cf1fe-a994-44e0-b03c-c5769f0d0d92', '7297c942-159e-491f-9a63-fc10420c1ebb', 'dbe6e93c-28e0-4b54-b4d5-3a0f4083059d', '199b91db-8921-42a2-a741-1dd323c09600']}>

session index  <Storage {'flash': None, '_formkey[register]': ['cd76b587-db09-46fa-a05e-b31c2f71d4c4', 'ad7148b5-3306-455b-9c2d-65e6c91e9856', '069640cf-0c68-416c-b091-8e8efa337e24', 'd89158de-86a5-4e23-9a07-43ba042a09ac', 'fa1a99ba-8d97-49c2-ad0d-ffe7ba66e412', '5cfb4a89-8284-4879-9b1d-9bc923350771', '6b55ba7d-fa6f-4709-a8ca-0ec30e4f4455', 'c983f92b-10d6-47d7-b02f-a5bc7a4419b4', '7ff1a289-f9b4-47b6-92a2-da93fcdeb8ca', 'c298cda9-447b-47e4-b9b5-7b1a6f85645d'], '_formkey[login]': ['dc014554-b3a9-4ab7-8a4e-21600922d485', '4afe46f2-77a8-4e74-abc9-1d2d512a1d56', '3fa4d78a-4113-419f-ae90-5a39d3bac9f9', '171e7853-0f31-4e66-b8ef-1c8c665610b5', '86b76af0-3f51-4f47-95e6-903a117db2cb', 'f117d59d-7e38-4e90-a70c-fb9f507b3681', '756458fd-1f13-4aab-9f71-fae5322199c5', 'e5949f3d-3094-45f2-94ba-ff4c9ea27d08', '8abea09c-9493-499b-bf50-5487262a9a03', '467f8079-508a-4c83-bbf1-49ba86ade0f1'], '_formkey[auth_user/create]': ['5cf42aee-c3b1-4f2b-a4b5-6027ec55c7c3', '6a58a734-dc76-43b0-84df-fc35444a6364', '0cc70146-e946-411c-9ced-4552098a07c8', 'b189dc4c-d789-463b-a223-ce22bb880828', '4ebe96ab-1b25-4bcf-98bd-285f51b28168', 'd5f15dea-51c4-446e-a66e-f3aaccc3937b', 'a13cf1fe-a994-44e0-b03c-c5769f0d0d92', '7297c942-159e-491f-9a63-fc10420c1ebb', 'dbe6e93c-28e0-4b54-b4d5-3a0f4083059d', '199b91db-8921-42a2-a741-1dd323c09600']}>


also when redirected to index.html the session is printed twice

Any suggestions why this kind of behaviour?

Anthony

unread,
May 7, 2017, 9:18:16 AM5/7/17
to web2py-users
Are you successfully able to show any flash messages (i.e., simply set response.flash within any action and see if it shows up on the page)? Is the appropriate DIV in the layout.html?

Note, by the time you are in the index function, session.flash is not expected to be there any longer, as the framework copies session.flash to response.flash after a redirect. Is there anything in response.flash at that point?

Anthony

Yebach

unread,
May 8, 2017, 5:08:59 AM5/8/17
to web2py-users
Flash is show on my index page so the position <div class="flash">{{=session.flash or ''}}</div> is ok
I just put session.flash('Some random text') in my index function and it works

But after registration the message is not shown

My code for user registration in index.py user

if request.args(0) == 'register':

#form = auth.register()
if form.process().accepted:
        #some code
        session.flash = ('Thank you for registering. Please check your e-mail account')
        redirect(URL(request.application, 'default', 'index'))

Anthony

unread,
May 8, 2017, 8:34:50 AM5/8/17
to web2py-users
On Monday, May 8, 2017 at 5:08:59 AM UTC-4, Yebach wrote:
Flash is show on my index page so the position <div class="flash">{{=session.flash or ''}}</div> is ok
I just put session.flash('Some random text') in my index function and it works

session.flash is not intended to work that way. You use session.flash only when you are doing a redirect, in which case, it gets moved from session.flash to response.flash after the redirect. So, the template should have {{=response.flash}}, not {{=session.flash}}.
 
My code for user registration in index.py user

if request.args(0) == 'register':

#form = auth.register()
if form.process().accepted:
        #some code
        session.flash = ('Thank you for registering. Please check your e-mail account')
        redirect(URL(request.application, 'default', 'index'))

How is "form" defined above? If it is defined as form=auth.register(), then you cannot subsequently call form.process(), as auth.register() already calls form.process().

Anthony

Vid Ogris

unread,
May 9, 2017, 4:15:13 AM5/9/17
to web...@googlegroups.com
Hello

Form is defined as form = auth()

Changing session.flash to response.flash still does not work after user


This is my whole user code

def user():
    form = auth()

    if request.args(0)=='profile':
        #uporabnikov Id
        uid =  auth.user_id
        #preberem podatke za tega userja iz obeh tabel
        record = db((db.auth_user.organization==db.organizations.id) & (db.auth_user.id == uid)).select().as_list()[0]
        recordd = db((db.auth_user.organization==db.organizations.id) & (db.auth_user.id == uid)).select().first()
        
        #Skrijem nepotrebna polja
        fields_to_hide_org = ['id', 'o_usern', 'o_useru', 'o_daten','o_dateu', 'o_status', 'o_code', 'o_faxnumber',\
                          'o_rootid', 'o_parentid', 'o_levelid', 'o_positionx','o_positiony' ]
        
        for fieldname in fields_to_hide_org:
            field = db.organizations[fieldname]
            field.readable = field.writable = False
        
        fields_to_hide_user = ['email', 'password', 'organization']
        
        for fieldname in fields_to_hide_user:
            field = db.auth_user[fieldname]
            field.readable = field.writable = False
        
        #org_name =  db(db.organization.id == record["organization"]).select(db.organization.o_name).as_list()[0]["o_name"]
        
       
        #nastavim vrednsoti polj, ki so ze v bazi
        authfields = [field for field in db.auth_user if field.name != 'id']
        
        #print record
        for field in authfields:
            #print field
            field.default = recordd.auth_user[field.name]
        
        orgfields = [field for field in db.organizations if field.name != 'id']
        for field in orgfields:
            field.default = recordd.organizations[field.name]

        
        form=SQLFORM.factory(db.auth_user,db.organizations, keepvalues = True)
        
        orgid = record["organizations"]['id']
        #print orgid
        
        if form.process().accepted:
            #Updatam polja v bazi
            org_forma = db.organizations._filter_fields(form.vars)
            for key, value in org_forma.iteritems():
                #print key, value
                db(db.organizations._id==orgid).update(**{key:value})
                #db(db.organization.id==orgid).update(key = value)
            
            user_forma = db.auth_user._filter_fields(form.vars)
            for key, value in user_forma.iteritems():
                db(db.auth_user._id==orgid).update(**{key:value})
             
            
            session.flash=T('Thanks for filling the form')
            redirect(URL('index'))
            
        return dict(form=form)
    
    
    
    """
    Tole je sedaj tukaj ker ni delal foreing key na auth_user.organization
    """    
    if request.args(0) == 'register':

        #form = auth.register()
        if form.process().accepted:
            database = str(db).rpartition('/')[-1].replace('">', '')
            host = request.http_host
            port = request.http_port
            app = request.application
            if host:
                name = '%s/%s' % (host, app)
            else:
                name = app
            #print form.vars
            userid =  form.vars.id
            email = form.vars.email
            first_name = form.vars.first_name
            last_name =form.vars.last_name
            org_name = form.vars.organization
            add_organization(form, userid)

            #glede na jezik se izbere formular
            if any("adminLanguage=sl" in s for s in request.env.HTTP_COOKIE.split(";")):
                formFile = os.path.join(request.folder, 'private', 'formular_WoShi_podatki.doc')
            else:
                formFile = os.path.join(request.folder, 'private', 'form_WoShi_data.doc')


            if not formFile or formFile is None:
                attachment = None
            else:
                attachment = mail.Attachment(formFile, content_id='file')

            #print URL(args=request.args, vars=request.get_vars, host=True)

            app_Url = '%s://%s' % (request.env.wsgi_url_scheme, request.env.http_host)
            #print app_Url

            regKey = db(db.auth_user.id == userid).select(db.auth_user.registration_key).first()
            key = regKey.registration_key
            message_content = T('Click on the link ') + app_Url + URL(r=request,c='default',f='user',args=['verify_email']) +  '/%s ' % (key)  + T('to verify your email')
            #print message_content

            mail.send(to=[email],
                        subject='Woshi confirm registration',
                        message=message_content,
                        attachments = attachment
                        )
            
            ##dodamo še mail za in...@algit.si
            mail.send(to = 'somerandommail',
                      subject= 'New user registered in WoShi',
                      message = 'A new user '+ first_name + " " + last_name +' with e-mail address ' + email +" for organization " + org_name
                                + ' has registered to Woshi with url address '
                                + name + ' in database ' + database
                     )

            #session.flash = T('Thank you for registering. Please check your e-mail account')
            response.flash = T('Thank you for registering. Please check your e-mail account for further instructions')

            #auth.settings.register_onaccept=[session.flash]
            #session.flash=response.flash
            #print response.flash
            #print session
            redirect(URL(request.application, 'default', 'index'))
            #redirect(URL('index'))

    return dict(form = form) 

--
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/Yfvnke6ABWM/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.



--
Lep pozdrav 

Vid Ogris


Anthony

unread,
May 9, 2017, 10:17:57 AM5/9/17
to web2py-users
No, you should still set session.flash in your controller code. However, your view should have {{=response.flash}}, as the framework will move the value of session.flash to response.flash after the redirect.

Also, as already noted, you cannot define form=auth() and then subsequently call form.process().

Anthony

Vid Ogris

unread,
May 10, 2017, 3:14:08 AM5/10/17
to web...@googlegroups.com
How do you suggest I arrange my controller for user?

Anthony

unread,
May 10, 2017, 3:14:23 PM5/10/17
to web2py-users
On Wednesday, May 10, 2017 at 3:14:08 AM UTC-4, Yebach wrote:
How do you suggest I arrange my controller for user?

That's up to you, but for starters, maybe instead of putting all the logic in "if" blocks, make use of the Auth callback settings, such as auth.settings.register_onaccept. And for customizing the flash messages, use the auth.messages, such as auth.messages.registration_successful.

Also, if form.process() has already been called, as in the case of the form object returned by auth(), you can check whether it has been accepted by doing:

if form.accepted:

You don't have to (and should not) call form.process() a second time.

Anthony

Vid Ogris

unread,
May 11, 2017, 2:57:24 PM5/11/17
to web...@googlegroups.com
OK so changing if form.process().accepted to if form.accepted works for user registers, if I remove it from if request.args(0)=='profile':
block, it is not triggered. 
As far as flash message goes the flash works after user updates profile and everywhere else on my page but after registration it seems to hate my code and it decided to stay hidden/none :)

Thank you anyway




--
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/Yfvnke6ABWM/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.

Yebach

unread,
May 24, 2017, 7:39:06 AM5/24/17
to web2py-users
I do not know i it is a bug or smth but if I do not use 

       
if form.process().accepted:

and just use
if form.accepted:


Then add_organization(form, userid) is not triggered and also if I put some prints in it I cannot see them in my PyCharm console, while, now this is interesting, e-mail is sent.

Any suggestions or ideas?

if request.args(0) == 'register':


        form
= auth.register()
       
if form.process().accepted:

            database
= str(db).rpartition('/')[-1].replace('">', '')

            host
= request.http_host
            port
= request.http_port
            app
= request.
application
           
print database, host, app
           
if host:

                name
= '%s/%s' % (host, app)
           
else:
                name
=
app
            userid
=  form.vars.id

            email
= form.vars.email
            first_name
= form.vars.first_name
            last_name
=form.vars.last_name
            org_name
= form.vars.organization


            add_organization
(form, userid)

            mail
.send(to = 'someran...@domain.eu',

                      subject
= 'New user registered in WoShi',
                      message
= 'A new user '+ first_name + " " + last_name +' with e-mail address ' + email +" for organization " + org_name
                               
+ ' has registered to Woshi with url address '
                               
+ name + ' in database ' + database
                     
)



            session
.flash = T('Thank you for registering. Please check your e-mail account')
           
            redirect
(URL(request.application, 'default', 'index'))
   
return dict(form = form)
Reply all
Reply to author
Forward
0 new messages