Sign up user

Let's move on to create our sign up page to help register new users so that they can tweet as well.

Let's create signup.html, which will ask for user details. Check the following code snippet for this:

     <div class="container"> 
      <div class="row"> 
        <center><h2>Sign up</h2></center> 
          <div class="col-md-4 col-md-offset-4"> 
              <form method=POST action="{{ url_for('signup') }}"> 
                  <div class="form-group"> 
                      <label >Username</label> 
                      <input type="text" class="form-control"
name="username" placeholder="Username"> </div> <div class="form-group"> <label >Password</label> <input type="password" class="form-control"
name="pass" placeholder="Password"> </div> <div class="form-group"> <label >Email</label> <input type="email" class="form-control"
name="email" placeholder="email"> </div> <div class="form-group"> <label >Full Name</label> <input type="text" class="form-control"
name="name" placeholder="name"> </div> <button type="submit" class="btn btn-primary btn-
block">Signup</button> </form> <br> </div> </div> </div>

The preceding code is, basically, the template which needs the backend API to submit the data to the user.

Let's create a signup route, which will take the GET and POST methods to read the page, and submit the data to the backend database. The following is the code snippet which needs to be added to app.py:

    @app.route('/signup', methods=['GET', 'POST']) 
    def signup(): 
      if request.method=='POST': 
        users = mongo.db.users 
        api_list=[] 
        existing_user = users.find({'$or':  
[{"username":request.form['username']} ,
{"email":request.form['email']}]}) for i in existing_user: api_list.append(str(i)) if api_list == []: users.insert({ "email": request.form['email'], "id": random.randint(1,1000), "name": request.form['name'], "password": bcrypt.hashpw(request.form['pass'].
encode('utf-8'), bcrypt.gensalt()), "username": request.form['username'] }) session['username'] = request.form['username'] return redirect(url_for('home')) return 'That user already exists' else : return render_template('signup.html')

Once the user has signed up, it will set the session, and redirect it to your home page.

Your Sign up page should look something like this:

We have authenticated the user, but what if he wants to update his/her personal information? Let's create a profile page, which will help them do so.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.0.91