-1

Hey can anyone tell me why this code is not returning the get method username on top of the page. When you enter the submit button I want that the welcome pass contains the parameter for the username in the url. Example: for username="thisisme" the url we should be getting should be "....\welcome?username=thisisme". I am working this on the Google app engine.

import webapp2
import cgi
form="""
form method="post">
    <h2>This is the SignUp Page</h2>
    <br>
    <div><label>&emsp;&emsp;&emsp;Username: <input type="text" name="username" value="%(name)s"></label><span style="color:red">%(error1)s</span></div>
    <div><label>&emsp;&emsp;&emsp;&nbsp;Password: <input type="password" name="password" value="%(pas)s"></label><span style="color:red">%(error2)s</span></div>
    <div><label>&nbsp;&nbsp;Verify Password: <input type="password" name="verify" value="%(vpas)s"></label><span style="color:red">%(error3)s</span></div>
    <div><label>&nbsp;&nbsp;Email (optional): <input type="text" name="email" value="%(email)s"></label><span style="color:red">%(error4)s</span></div>
    <br><br>
    <input type="submit">
</form>"""

def escape_html(s):
    return cgi.escape(s, quote = True)

def valid_name(n):
    valid=set("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-_")
    if set(n).issubset(valid):
        return n
    else:
        return None

def valid_pas(p):
    if p.__len__()<6:
        return None
    else:
        return p

def valid_vpas(v, p):
    if v==p:
        return v
    else:
        return None

def valid_email(e):
    if '@gmail.com' in e or '@hotmail.com' in e or '@illinois.edu' in e or '@yahoo.com' in e or ''==e:
        return True
    else:
        return None

class MainHandler(webapp2.RequestHandler):
    def write_form(self, error1="", error2="", error3="", error4="", name="", pas="", vpas="", email=""):
        self.response.write(form % {"error1":error1, "error2":error2, "error3":error3, "error4":error4, "name":escape_html(name), "pas":escape_html(pas), "vpas":escape_html(vpas), "email":escape_html(email)})

    def get(self):
        self.write_form()

    def post(self):
        a1=" "
        a2=" "
        a3=" "
        a4=" "
        flag=0
        global user_name
        user_name=self.request.get('username')
        user_pas=self.request.get('password')
        user_vpas=self.request.get('verify')
        user_email=self.request.get('email')
        name=valid_name(user_name)
        pas=valid_pas(user_pas)
        vpas=valid_vpas(user_vpas, user_pas)
        email=valid_email(user_email)
        if not name:
            a1="Please enter no special characters or white spaces in this field"
            flag=1
        if not pas:
            a2="Please enter a valid password six characters long"
            flag=1
        if not email:
            a4="Don't enter it or Enter @gmail.com, @yahoo.com, @hotmail.com or @illinois.edu"
            flag=1
        if not vpas:
            a3="The passwords don't seem to match sir"
            flag=1
        if flag==1:
            self.write_form(a1, a2, a3, a4, user_name, user_pas, user_vpas, user_email)
            flag=0
        else:
            self.redirect("/welcome")

class SignHandler(webapp2.RequestHandler):
    def get(self):
        self.response.write('Welcome, {}'.format(user_name))

app = webapp2.WSGIApplication([('/', MainHandler), ('/welcome', SignHandler)], debug=True)
sid
  • 9
  • 2
  • May be because it's a POST request? – Ankit Jaiswal Nov 21 '13 at 07:26
  • If I use the get method then It shows me all the errors already and then it shows me all the parameters username, password, email in the url. Also it doesn't redirect to the signup page instead remains in the same place. – sid Nov 21 '13 at 16:45

2 Answers2

1

This really doesn't seem to be a server-side, problem, but a client side code problem that you have set for the form.

instead of

<form method='post'

use

<form method='get'

EDIT: If you want just the username in the URL, use jquery to submit the form dynamically to

your_url?username=username

You can get help from here: Submit a form using jQuery

EDIT 2: Here's what you need to insert in your html.. Paste it just before the form tag starts in order to use jquery the way you want to use it. :

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
    $(document).ready(function(){
        $("form:first").submit(function(){
            un = $("form:first input[name='username']").val();
            $("form:first").attr('action',location.href + "?username=" + un);
        });
    });
</script>

Generally, this should be within the head tags, but anyway..

Community
  • 1
  • 1
Naveed Hasan
  • 641
  • 7
  • 18
0

the angular bracket is missing

instead of

form>

use

<form

and you cannot expect it to have username in the the URL.