Flask CSS JSON SESSION





app.py

from flask import Flask,render_template,url_for,redirect,request,json,session

app=Flask(__name__)
app.secret_key= b'abcd1234'

@app.route('/')
def index():
return render_template('index.html')

@app.route('/second')
def test():
return render_template('test.html')

@app.route('/third')
def testThird():
return redirect(url_for('index'))

@app.route('/postExample',methods=['POST','GET'])
def postExample():
    if request.method == 'POST':
        if request.values['send']== '送出':
            return render_template('postExample.html',name=request.values['user'])
    return render_template('postExample.html',name="")

@app.route('/register',methods=['POST','GET'])
def register():
with open('./member.json','r') as file_object:
member = json.load(file_object)
if request.method=='POST':
if request.values['send']=='送出':
if request.values['userid'] in member:
for find in member:
if member[find]['nick']==request.values['username']:
return render_template('register.html',alert='this account and nickname are used.')
return render_template('register.html',alert='this account is used.',nick=request.values['username'])
else:
for find in member:
if member[find]['nick']==request.values['username']:
return render_template('register.html',alert='this nickname are used.',id=request.values['userid'],pw=request.values['userpw'])
member[request.values['userid']]={'password':request.values['userpw'],'nick':request.values['username']}
with open('./member.json','w') as f:
json.dump(member, f)
return render_template('index.html')
return render_template('register.html')

@app.route('/login',methods=['GET','POST'])
def login():
if request.method== 'POST' :
with open('./member.json','r') as file_object:
member = json.load(file_object)
            
if request.values['userid'] in member:
if member[request.values['userid']]['password']==request.values['userpw']:
session['username']=request.values['userid']
return redirect ( url_for ( 'index' ))
else:
return render_template('login.html',alert="Your password is wrong, please check again!")
else:
return render_template('login.html',alert="Your account is unregistered.")
return render_template('login.html')

@app.route('/logout',methods=['GET','POST'])
def logout ():
if request.method=='POST':
if request.values['send']=='確定':
session.pop('username',None)
return redirect(url_for('index'))
return render_template('logout.html')    
 
 
if __name__ == '__main__':
app.run(host='0.0.0.0',port='5000',debug=True)


register.html


<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" type="text/css" href="{{url_for('static',filename='css/nav_bar.css')}}">
<title>ironman album</title>
</head>
<style>
body{
margin: 0;
font-family: Arial, Helvetica, sans-serif;
}

table{
background-color:plum;
border-radius:5px;
padding-bottom: 10px;
padding-top: 15px;
padding-right: 10px;
margin-top:20px;
width:70%;
}

input{
font-size: initial;
}

span {
background-color:#FFC8B4;
box-shadow:1px 1px 3px red;
margin-right:8px;
}

@media screen and (max-width: 400px){
table{
width:90%;
}
}

.button {
  border:0;
  background-color:darkviolet;
  color:#fff;
  border-radius:20px;
  cursor:pointer;
  width:90px;
  height:30px;
}

.button:hover{
  color:black;
  background-color: lavender;
}
</style>
<body>
<form method="post">
<ul class="computer" id="topnav">
<li><a href="../" class="active">首頁</a></li>
<li><a href="../album">相簿</a></li>
<li><a href="../register">註冊</a></li>
<li><a href="../login">登入</a></li>
<li style="float:right"><a href="javascript:void(0);" class="icon" onclick="myFunction()"><i class="fa fa-bars"></i></a></li>
</ul>
<div style="padding-top: 100px">    
  <table align="center">
    <tr>
    <td colspan="2" align="right"><span>{{alert}}</span></td>
    </tr>
    <tr>
      <th width="15%">account</th>
      <td><input class="w3-input w3-border w3-round-large" type='text' name="userid" value={{id}}></td>
    </tr>
    <tr>
      <th>password</th>
      <td><input class="w3-input w3-border w3-round-large" type='password' name="userpw" value={{pw}}></td>
    </tr>
    <tr>
      <th>Nick Name</th>
      <td><input class="w3-input w3-border w3-round-large" type='text' name="username" value={{nick}}></td>
    </tr>
    <tr>
      <td colspan="2" align="right"><input class="button" value="送出" type='submit' name="send"></td>
    </tr>
  </table>
</div>


</form>
<script>
function myFunction() {
  var x = document.getElementById("topnav");
  if (x.className === "computer") {
    x.className = "phone";
  } else {
    x.className = "computer";
  }
}
</script>


</body>
</html>

login.html

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" type="text/css" href="{{url_for('static',filename='css/nav_bar.css')}}">
<title>ironman album</title>
</head>
<style>
body{
margin: 0;
font-family: Arial, Helvetica, sans-serif;
}

table{
background-color:plum;
border-radius:5px;
padding-bottom: 10px;
padding-top: 15px;
padding-right: 10px;
margin-top:20px;
width:70%;
}

input{
font-size: initial;
}

span {
background-color:#FFC8B4;
box-shadow:1px 1px 3px red;
margin-right:8px;
}

@media screen and (max-width: 400px){
table{
width:90%;
}
}

.button {
  border:0;
  background-color:darkviolet;
  color:#fff;
  border-radius:20px;
  cursor:pointer;
  width:90px;
  height:30px;
}

.button:hover{
  color:black;
  background-color: lavender;
}
</style>
<body>
<ul class="computer" id="topnav">
<li><a href="../" class="active">首頁</a></li>
<li><a href="../album">相簿</a></li>
<li><a href="../register">註冊</a></li>
<li><a href="../login">登入</a></li>
<li style="float:right"><a href="javascript:void(0);" class="icon" onclick="myFunction()"><i class="fa fa-bars"></i></a></li>
</ul>
<form method="post">
<div style="padding-top: 100px">    
  <table align="center">
    <tr>
    <td colspan="2" align="right"><span>{{alert}}</span></td>
    </tr>
    <tr>
      <th width="15%">account</th>
      <td><input class="w3-input w3-border w3-round-large" type='text' name="userid" value={{id}}></td>
    </tr>
    <tr>
      <th>password</th>
      <td><input class="w3-input w3-border w3-round-large" type='password' name="userpw" value={{pw}}></td>
    </tr>
    <tr>
      <td colspan="2" align="right"><input class="button" value="登入" type='submit' name="send"></td>
    </tr>
  </table>
</div>
</form>
<script>
function myFunction() {
  var x = document.getElementById("topnav");
  if (x.className === "computer") {
    x.className = "phone";
  } else {
    x.className = "computer";
  }
}
</script>


</body>
</html>


logout.html

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" type="text/css" href="{{url_for('static',filename='css/nav_bar.css')}}">
<title>ironman album</title>
</head>
<style>
body{
margin: 0;
font-family: Arial, Helvetica, sans-serif;
}

table{
background-color:plum;
border-radius:5px;
padding-bottom: 10px;
padding-top: 15px;
padding-right: 10px;
margin-top:20px;
width:70%;
}

input{
font-size: initial;
}

span {
background-color:#FFC8B4;
box-shadow:1px 1px 3px red;
margin-right:8px;
}

@media screen and (max-width: 400px){
table{
width:90%;
}
}

.button {
  border:0;
  background-color:darkviolet;
  color:#fff;
  border-radius:20px;
  cursor:pointer;
  width:90px;
  height:30px;
}

.button:hover{
  color:black;
  background-color: lavender;
}
</style>
<body>
<form method="post">
<ul class="computer" id="topnav">
<li><a href="../" class="active">首頁</a></li>
<li><a href="../album">相簿</a></li>
<li><a href="../upload">上傳</a></li>
  <li><a href="../logout">登出</a></li>
  <li style="float:right"><a href="javascript:void(0);" class="icon" onclick="myFunction()"><i class="fa fa-bars"></i></a></li>
</ul>
<div style="padding-top: 80px;padding-left:20px">
  <h2>確認登出嗎?</h2>
  <p> </p>
  <p><input class="button" value="確定" type='submit' name="send">
  <input class="button" value="取消" type='submit' name="send"></p>
</div>


</form>
<script>
function myFunction() {
  var x = document.getElementById("topnav");
  if (x.className === "computer") {
    x.className = "phone";
  } else {
    x.className = "computer";
  }
}
</script>


</body>
</html>

json

{"carlos": {"nick": "carloshphk", "password": "1234"}, "yueh860304": {"nick": "Charlotte", "password": "1234"}, "yueh970304": {"nick": "Longwind", "password": "1234"}}





留言

這個網誌中的熱門文章

香港袐密行動

要老是忘記, 我更記不起