1. 在首頁新增顯示問答的列表,並定義好相應的樣式。
無序列表
2. 用字典向index.html傳遞引數。
python**
from flask import
flask, render_template, request, redirect, url_for, session
from flask_sqlalchemy import
sqlalchemy
from functools import
wraps
from datetime import
datetime
import
config
)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)
telphone = db.column(db.string(20), nullable=true)
class
question(db.model):
__tablename__ = '
question
'id = db.column(db.integer, primary_key=true, autoincrement=true)
author_id = db.column(db.integer,db.foreignkey('
user.id'))
title = db.column(db.string(100), nullable=false)
detail = db.column(db.text, nullable=false)
creat_time = db.column(db.datetime, default=datetime.now)
author = db.relationship('
user
', backref=db.backref('
question'))
#db.create_all()'/
',methods=['
get','
post'])
defindex():
context =
return render_template('
index.html
',**context)
'/login/
', methods=['
get', '
post'])
deflogin():
if request.method == '
get'
:
return render_template('
login.html')
else
: usern = request.form.get('
username')
passn = request.form.get('
password')
user = user.query.filter(user.username ==usern).first()
ifuser:
if user.password ==passn:
session[
'user
'] =usern
return redirect(url_for('
index'))
else
:
return
'密碼錯誤'#
return window.alert("密碼錯誤")
else
:
#return window.alert("使用者名稱不存在")
return
'使用者名稱不存在
'def
mycontext():
usern = session.get('
user')
ifusern:
return
else
:
return
{}def
login_re(func):
@wraps(func)
if session.get('
user'):
return func(*args, **kwargs)
else
:
return redirect(url_for('
login'))
return
'/logout/')
deflogout():
session.clear()
return redirect(url_for("
index"))
'/register/
', methods=['
get', '
post'])
defregister():
if request.method == '
get'
:
return render_template('
register.html')
else
: usern = request.form.get('
username')
passn = request.form.get('
password')
teln = request.form.get('
telphone')
user = user.query.filter(user.username ==usern).first()
ifuser:
return
'使用者名稱已經存在
'else
: user1 = user(username=usern, password=passn, telphone=teln)
db.session.add(user1)
db.session.commit()
return redirect(url_for('
login'))
'/question/
', methods=['
get', '
post'])
@login_re
defquestion():
if request.method == '
get'
:
return render_template('
question.html')
else
: an = user.query.filter(user.username == session.get('
user
')).first().id
tn= request.form.get('
title')
dn = request.form.get('
detail')
question1 = question(author_id=an,title=tn, detail=dn)
#question.author=user
db.session.add(question1) #
資料庫,新增操作
db.session.commit() #
提交return redirect(url_for('
ecshop模板首頁或列表頁顯示商品簡單描述
1 在ecshop首頁顯示商品簡單描述 依次開啟 themes default library recommend hot.lbi themes default library recommend new.lbi themes default library recommend best.lbi 檔...
ecshop模板首頁或列表頁顯示商品簡單描述
1 在ecshop首頁顯示商品簡單描述 依次開啟 themes default library recommend hot.lbi themes default library recommend new.lbi themes default library recommend best.lbi 檔...
Tomcat設定顯示首頁問題
spring遇見問題 一 預設首頁顯示問題 最近在搭建乙個web專案,我發現怎麼首頁總是404錯誤,找不到呢?就查了一下標籤的使用,發現這個標籤早在tomcat中就已經定義好了,而在自己的web工程進行修改時,並沒有覆蓋之前的配置。tomcathome conf web.xml index.html...