Day 084 - Project 84 : Create users, log in

If you have any questions, comments or issues with this project please post them here!

Hi,

I finished this project, here’s my repl: https://replit.com/@masterneme/Day84100Days

I used the default example for the challenge which redirects to different gifs depending on success and failure logging in.

I wanted a customized greeting while keeping the redirects, therefore, in line 25 I pass the full name so in lines 51-53 I can add it to the page.

I want to know if there’s a more elegant way to do this so the name doesn’t show up in the URL.

1 Like

Hi @masterneme thanks for sharing your project and well done on your progress so far in
#100-days-of-code

Have you investigated session variables to store the username? Session data in Python Flask - Python Tutorial has some more information on this.

Yeah I learned about sessions the next day :smiley:

So I’ll leave the project as it is for future reference, thanks.

1 Like

I was wondering the same thing… then I came upon your repl and it’s not a bad idea… but again, I’m facing with issue of how to return some string AND then redirect to another page… any thoughts on that? Thanks

Sorry if I spam but for some reason, signup page doesn’t add new users, returns Bad Request when trying to create new user… even when I made my code same as in david’s video… :confused: here’s a link to my repl if anyone would like to take a look…
https://replit.com/@Thrsh001/Day84100Days

EDIT: Okay… my bad… I had the “password” in signup form capitalized… :sweat_smile:

Hello! I think my code is right, but idk it doesn’t work. After Sigh Up it doen’t go to Login page. I need help, please!

from flask import Flask, request, redirect, render_template
from replit import db

app = Flask(__name__)

@app.route('/signup', methods=["POST"])
def sign():
  form = request.form
  keys = db.keys()
  if form['username'] not in keys:
    db[form["username"]] = {"name": form["name"], "password": form["password"]}
    return redirect('/login')
  else:
    return redirect('login')

@app.route('/login', methods=["POST"])
def check():
  form = request.form
  keys = db.keys()
  if form['username'] not in keys:
    return redirect('/login')
  else:
    if db[form["username"]]["password"]==form["password"]:
      return redirect(f'/welcome-{db[form["username"]]["name"]}')
    else:
      return redirect("/error")

@app.route('/signup')  #  SIGN IN
def signup():
  page = ""
  f = open("signup.html", "r")
  page = f.read()
  f.close()
  return page

@app.route('/login')  #  LOG IN
def login():
  page = ""
  f = open("login.html", "r")
  page = f.read()
  f.close()
  return page

@app.route("/error")
def error():
  return """Error. Username or password are invalid."""

@app.route("/welcome<name>")
def welcome(name):
  return f'Hello {name}'

@app.route('/')
@app.route('/<string:page_name>')
def page(page_name='main.html'):
  return render_template(page_name)

app.run(host='0.0.0.0', port=81)

1 Like

In

You’ve forgotten the / before login.

sorry for longer no reply! ty!

2 Likes