<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>fire</title>
<link rel="stylesheet" type="text/css" href="../static/css/20.css">
<link rel="stylesheet" type="text/css" href="../static/css/200.css">
<script src="../static/js/39.js"></script>
<style type="text/css">
</style>
</head>
<body class="abc">
<h1 align="center"><span style="font-size:100px;color:lightskyblue">美丽の物</span></h1>
<nav style="margin:0" align="center" >
<a href={{ url_for('index') }}>INDEX</a>
<input type="text"name="search">
<button type="submit">SEARCH</button>
</span>
{% if username %}
<a href="#">{{ username }}</a>
<a href="{{ url_for('logout') }}">logout</a>
{% else %}
<a href="{{ url_for('login') }}">LOGIN</a>
<a href="{{ url_for('rege') }}">REGIST</a>
{% endif %}
<a href={{ url_for('question') }}>QUESTION</a>
</nav>
{% block question %}{% endblock %}
{% block rege %}{% endblock %}
{% block login %}{% endblock %}
{% block index %}{% endblock %}
</body>
</html>
from flask_sqlalchemy import SQLAlchemy
import config
from flask import Flask,render_template,request,redirect, url_for,session
app = Flask(__name__)
app.config.from_object(config)
db=SQLAlchemy(app)
class User(db.Model):
__tablename__ = 'User'
id = db.Column(db.Integer,primary_key=True,autoincrement=True)
username = db.Column(db.String(20),nullable=False)
password = db.Column(db.String(20),nullable=False)
nickname = db.Column(db.String(20))
#db.create_all()
@app.route('/')
def index():
return render_template('index.html')
@app.route('/login/',methods=['GET','POST'])
def login():
if request.method == 'GET':
return render_template('login.html')
else:
usern = request.form.get('username')
passw = request.form.get('password')
user = User.query.filter(User.username == usern).first()
if user:
if user.password==passw:
session['user'] = usern
session.permanent = True
return redirect(url_for('index'))
else:
return u'password error'
else:
return u'password is not existed'
@app.route('/rege/',methods=['GET','POST'])
def rege():
if request.method =='GET':
return render_template('rege.html')
else:
usern = request.form.get('username')
passw = request.form.get('password')
nickn = request.form.get('nickname')
user = User.query.filter(User.username ==usern).first()
if user:
return 'username.existed.'
else:
user1 = User(username=usern,password=passw,nickname = nickn)
db.session.add(user1)
db.session.commit()
return redirect(url_for('login'))
@app.route('/question/')
def question():
return render_template('question.html')
@app.context_processor
def mycontext():
usern=session.get('user')
if usern:
return{'username':usern}
else:
return{}
@app.route('/logout/')
def logout():
session.clear()
return redirect(url_for('index'))
if __name__=="__main__":
app.run(debug=True)