v1.2.0 完成了网页的结构内容,需要css进行美化

This commit is contained in:
yilv committed 2024-06-13 20:03:42 +08:00
1 parent a710d7de9e
commit 76777653cf
24 files changed
+596 -187

No files matched your search

Binary file not shown.
-18
View File
@@ -1,18 +0,0 @@
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def home():
return render_template('index.html')
@app.route('/signin')
def signin():
return render_template('signin.html')
@app.route('/signup')
def signup():
return render_template('signup.html')
if __name__ == '__main__':
app.run(debug=True)
-28
View File
@@ -1,28 +0,0 @@
from io import BytesIO
from PIL import Image, ImageDraw, ImageFont
import random
import string
def generate_captcha():
# Create an image with white background
image = Image.new('RGB', (100, 40), (255, 255, 255))
draw = ImageDraw.Draw(image)
# Choose a font and size
font = ImageFont.truetype("arial.ttf", 24)
# Generate random captcha text
text = ''.join(random.choices(string.ascii_letters + string.digits, k=5))
# Draw the text on the image
draw.text((10, 5), text, font=font, fill=(0, 0, 0))
# Distort the image (optional)
# Add more distortion if needed
# Save the image to a BytesIO object
output = BytesIO()
image.save(output, format='PNG')
output.seek(0)
return output
-7
View File
@@ -1,7 +0,0 @@
# Backend/config.py
import os
class Config:
SQLALCHEMY_DATABASE_URI = 'mysql+mysqlconnector://admin:LOpl23!!qq@localhost/website'
SQLALCHEMY_TRACK_MODIFICATIONS = False
SECRET_KEY = os.urandom(24)
-63
View File
@@ -1,63 +0,0 @@
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
box-sizing: border-box;
}
header {
background-color: #333;
color: white;
padding: 10px 0;
text-align: right;
}
header nav ul {
list-style: none;
margin: 0;
padding: 0;
}
header nav ul li {
display: inline;
margin-right: 20px;
}
header nav ul li a {
color: white;
text-decoration: none;
}
main {
padding: 20px;
}
form {
max-width: 400px;
margin: 0 auto;
}
form label {
display: block;
margin-top: 10px;
}
form input {
width: 100%;
padding: 8px;
margin-top: 5px;
box-sizing: border-box;
}
button {
margin-top: 20px;
padding: 10px 20px;
background-color: #333;
color: white;
border: none;
cursor: pointer;
}
button:hover {
background-color: #555;
}
-39
View File
@@ -1,39 +0,0 @@
document.addEventListener('DOMContentLoaded', () => {
const signinForm = document.querySelector('form[action="/signin"]');
if (signinForm) {
signinForm.addEventListener('submit', (event) => {
const password = document.getElementById('password').value;
const regex = /^[A-Za-z0-9]+$/;
if (!regex.test(password)) {
event.preventDefault();
alert('密码只能包含大小写字母和数字,不允许出现其他符号。');
}
});
}
const signupForm = document.querySelector('form[action="/signup"]');
if (signupForm) {
signupForm.addEventListener('submit', (event) => {
const password = document.getElementById('password').value;
const confirmPassword = document.getElementById('confirm-password').value;
const captcha = document.getElementById('captcha').value;
const lengthRegex = /^.{6,16}$/;
const alphanumericRegex = /^(?=.*[A-Za-z])(?=.*[0-9])[A-Za-z0-9]+$/;
if (!lengthRegex.test(password)) {
event.preventDefault();
alert('密码必须是6到16个字符之间。');
} else if (!alphanumericRegex.test(password)) {
event.preventDefault();
alert('密码必须包含字母和数字,不允许出现其他符号。');
} else if (password !== confirmPassword) {
event.preventDefault();
alert('密码和确认密码不匹配。');
} else if (!captcha) {
event.preventDefault();
alert('请填写验证码。');
}
});
}
});
-24
View File
@@ -1,24 +0,0 @@
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
<title>Homepage</title>
</head>
<body>
<header>
<nav>
<ul>
<li><a href="{{ url_for('signin') }}">Sign In</a></li>
<li><a href="{{ url_for('signup') }}">Sign Up</a></li>
</ul>
</nav>
</header>
<main>
<h1>Welcome to Our Website</h1>
<!-- Add more content here -->
</main>
<script src="js/script.js"></script>
</body>
</html>
Binary file not shown.
Binary file not shown.
BIN
View File
Binary file not shown.
+139
View File
@@ -0,0 +1,139 @@
from flask import Flask, render_template, request, redirect, url_for, session, send_file
import sqlite3
import os
from captcha import generate_captcha
from datetime import timedelta
app = Flask(__name__)
app.secret_key = 'your_secret_key'
DATABASE = 'app.db'
app.config['PERMANENT_SESSION_LIFETIME'] = timedelta(hours=1)
app.config['SESSION_COOKIE_SAMESITE'] = 'Lax'
def get_db_connection():
conn = sqlite3.connect(DATABASE)
conn.row_factory = sqlite3.Row
return conn
@app.route('/')
def index():
conn = get_db_connection()
posts = conn.execute('SELECT * FROM posts ORDER BY created_at DESC').fetchall()
conn.close()
return render_template('index.html', posts=posts)
@app.route('/signin', methods=['GET', 'POST'])
def signin():
if request.method == 'POST':
account = request.form['account']
password = request.form['password']
captcha = request.form['captcha']
if captcha != session.get('captcha', ''):
return 'Invalid captcha'
conn = get_db_connection()
user = conn.execute('SELECT * FROM users WHERE username = ? AND password = ?', (account, password)).fetchone()
conn.close()
if user:
session['user'] = account
return redirect(url_for('dashboard'))
else:
return 'Invalid credentials'
captcha_image, captcha_text = generate_captcha()
session['captcha'] = captcha_text
return render_template('signin.html', captcha_image=captcha_image)
@app.route('/signup', methods=['GET', 'POST'])
def signup():
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
confirm_password = request.form['confirm-password']
age = request.form['age']
captcha = request.form['captcha']
if password != confirm_password:
return 'Passwords do not match'
if captcha != session.get('captcha', ''):
return 'Invalid captcha'
conn = get_db_connection()
existing_user = conn.execute('SELECT * FROM users WHERE username = ?', (username,)).fetchone()
if existing_user:
conn.close()
return 'Username already exists'
conn.execute('INSERT INTO users (username, password, age) VALUES (?, ?, ?)', (username, password, age))
conn.commit()
conn.close()
return redirect(url_for('signin'))
captcha_image, captcha_text = generate_captcha()
session['captcha'] = captcha_text
return render_template('signup.html', captcha_image=captcha_image)
@app.route('/captcha')
def captcha():
captcha_image, captcha_text = generate_captcha()
session['captcha'] = captcha_text
return send_file(captcha_image, mimetype='image/png')
@app.route('/dashboard')
def dashboard():
if 'user' not in session:
return redirect(url_for('signin'))
return render_template('dashboard.html')
@app.route('/publish', methods=['POST'])
def publish():
if 'user' not in session:
return redirect(url_for('signin'))
content = request.form['content']
username = session['user']
conn = get_db_connection()
conn.execute('INSERT INTO posts (username, content) VALUES (?, ?)', (username, content))
conn.commit()
conn.close()
return redirect(url_for('index'))
@app.route('/delete_post/<int:post_id>', methods=['POST'])
def delete_post(post_id):
if 'user' not in session:
return redirect(url_for('signin'))
username = session['user']
conn = get_db_connection()
post = conn.execute('SELECT * FROM posts WHERE id = ?', (post_id,)).fetchone()
if post and post['username'] == username:
conn.execute('DELETE FROM posts WHERE id = ?', (post_id,))
conn.commit()
conn.close()
return redirect(url_for('user'))
@app.route('/user')
def user():
if 'user' not in session:
return redirect(url_for('signin'))
username = session['user']
conn = get_db_connection()
user_info = conn.execute('SELECT * FROM users WHERE username = ?', (username,)).fetchone()
user_posts = conn.execute('SELECT * FROM posts WHERE username = ? ORDER BY created_at DESC', (username,)).fetchall()
conn.close()
return render_template('user.html', user_info=user_info, user_posts=user_posts)
@app.route('/logout')
def logout():
session.pop('user', None)
return redirect(url_for('signin'))
@app.route('/aboutus')
def aboutus():
return render_template('aboutus.html')
if __name__ == '__main__':
app.run(debug=True)
+17
View File
@@ -0,0 +1,17 @@
from io import BytesIO
import random
import string
from PIL import Image, ImageDraw, ImageFont
def generate_captcha():
captcha_text = ''.join(random.choices(string.ascii_uppercase + string.digits, k=6))
image = Image.new('RGB', (120, 30), color = (255, 255, 255))
font = ImageFont.truetype("arial.ttf", 24)
draw = ImageDraw.Draw(image)
draw.text((10, 0), captcha_text, font=font, fill=(0, 0, 0))
buffer = BytesIO()
image.save(buffer, format="PNG")
buffer.seek(0)
return buffer, captcha_text
+25
View File
@@ -0,0 +1,25 @@
import sqlite3
connection = sqlite3.connect('app.db')
with connection:
connection.execute("""
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT NOT NULL UNIQUE,
password TEXT NOT NULL,
age INTEGER,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
""")
connection.execute("""
CREATE TABLE IF NOT EXISTS posts (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT NOT NULL,
content TEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
""")
print("Database and tables created successfully.")
View File
Whitespace-only changes.
File renamed without changes.
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 390 B

+226
View File
@@ -0,0 +1,226 @@
/* 导航栏以及body样式 */
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
box-sizing: border-box;
}
header {
background-color: #333;
color: white;
padding: 10px 0;
text-align: right;
}
header nav ul {
list-style: none;
margin: 0;
padding: 0;
}
header nav ul li {
display: inline;
margin-right: 20px;
}
header nav ul li a {
color: white;
text-decoration: none;
}
main {
padding: 20px;
}
form {
max-width: 400px;
margin: 0 auto;
}
form label {
display: block;
margin-top: 10px;
}
form input {
width: 100%;
padding: 8px;
margin-top: 5px;
box-sizing: border-box;
}
button {
margin-top: 20px;
padding: 10px 20px;
background-color: #333;
color: white;
border: none;
cursor: pointer;
}
button:hover {
background-color: #555;
}
#content-list {
margin-top: 20px;
}
.content-item {
border: 1px solid #ccc;
padding: 10px;
margin-bottom: 10px;
background-color: #f9f9f9;
}
.content-item strong {
display: block;
margin-bottom: 5px;
font-weight: bold;
}
/* Dashboard 样式 */
.dashboard {
max-width: 150%;
margin: 0 auto;
padding: 20px;
background-color: #f4f4f4;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
.dashboard h1 {
text-align: left;
margin-bottom: 20px;
}
.form-box {
width: 1800px;
background-color: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
.form-box label {
display: block;
margin-bottom: 10px;
}
.form-box textarea {
width: 90%;
height: 150px;
padding: 10px;
margin-bottom: 20px;
border: 1px solid #ddd;
border-radius: 4px;
resize: vertical;
}
.form-box button {
padding: 10px 20px;
background-color: #333;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
display: block;
width: 100%;
}
/* 用户界面样式 */
/* user.html */
/* 内容列表样式 */
#content-list {
margin-top: 20px;
}
.content-item {
border: 1px solid #ccc;
padding: 10px;
margin-bottom: 10px;
background-color: #f9f9f9;
display: flex;
justify-content: space-between;
align-items: center;
}
.content-item p {
margin: 0;
flex-grow: 1;
}
.content-item small {
margin-left: 10px;
color: #888;
}
.delete-form {
margin-left: 10px;
}
.delete-button {
background-color: #e74c3c;
color: white;
border: none;
border-radius: 4px;
padding: 5px 10px;
cursor: pointer;
}
.delete-button:hover {
background-color: #c0392b;
}
/* 用户界面样式 */
.user-profile {
max-width: 800px;
margin: 0 auto;
padding: 20px;
background-color: #f4f4f4;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
.user-profile h1 {
text-align: center;
margin-bottom: 20px;
}
.user-info {
margin-bottom: 20px;
}
.user-info p {
margin: 5px 0;
}
.user-posts {
margin-top: 20px;
}
.user-posts .content-item {
border: 1px solid #ccc;
padding: 10px;
margin-bottom: 10px;
background-color: #f9f9f9;
display: flex;
justify-content: space-between;
align-items: center;
}
.logout-button {
display: block;
text-align: center;
padding: 10px 20px;
background-color: #333;
color: white;
border: none;
border-radius: 4px;
text-decoration: none;
margin-top: 20px;
}
.logout-button:hover {
background-color: #555;
}
+38
View File
@@ -0,0 +1,38 @@
document.addEventListener('DOMContentLoaded', () => {
const signupForm = document.getElementById('signup-form');
if (signupForm) {
signupForm.addEventListener('submit', async (event) => {
event.preventDefault();
const formData = new FormData(signupForm);
const response = await fetch(signupForm.action, {
method: 'POST',
body: formData,
});
const text = await response.text();
if (text === 'Username already exists') {
alert('用户名已存在,请选择另一个用户名。');
} else if (text === 'Passwords do not match') {
alert('密码和确认密码不匹配。');
} else if (text === 'Invalid captcha') {
alert('验证码无效。');
} else {
window.location.href = '/signin';
}
});
}
const signinForm = document.querySelector('form[action="/signin"]');
if (signinForm) {
signinForm.addEventListener('submit', (event) => {
const password = document.getElementById('password').value;
const regex = /^[A-Za-z0-9]+$/;
if (!regex.test(password)) {
event.preventDefault();
alert('密码只能包含大小写字母和数字,不允许出现其他符号。');
}
});
}
});
+20
View File
@@ -0,0 +1,20 @@
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
<link rel="icon" href="{{ url_for('static', filename='icon.png') }}"> <!--添加图标-->
<title>about us</title>
</head>
<body>
<main>
<a href="{{ url_for('index')}}">return Homepage</a>
<h1>ABOUT US</h1>
<p>
</p>
</main>
<script src="{{ url_for('static', filename='js/script.js') }}"></script>
</body>
</html>
+30
View File
@@ -0,0 +1,30 @@
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
<link rel="icon" href="{{ url_for('static', filename='icon.png') }}"> <!--添加图标-->
<title>Dashboard</title>
</head>
<body>
<header>
<nav>
<ul>
<li><a href="{{ url_for('index') }}">HOMEpage</a></li>
<li><a href="{{ url_for('user') }}">user-profile</a></li>
<li><a href="{{ url_for('dashboard') }}">Dashboard</a></li>
</ul>
</nav>
</header>
<main class="dashboard">
<h1>Dashboard</h1>
<form action="/publish" method="post" class="form-box">
<label for="content">Content:</label>
<textarea id="content" name="content" required></textarea><br>
<button type="submit">Publish</button>
</form>
</main>
</body>
</html>
+40
View File
@@ -0,0 +1,40 @@
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
<link rel="icon" href="{{ url_for('static', filename='icon.png') }}"> <!--添加图标-->
<title>Homepage</title>
</head>
<body>
<header>
<nav>
<ul>
<li><a href="{{ url_for('signin') }}">Sign In</a></li>
<li><a href="{{ url_for('signup') }}">Sign Up</a></li>
<li><a href="{{ url_for('dashboard') }}">Dashboard</a></li>
{% if 'user' in session %}
<li><a href="{{ url_for('user') }}">User Profile</a></li>
{% endif %}
</ul>
</nav>
</header>
<main>
<h1>Welcome to Our Website</h1>
<div id="content-list">
{% for post in posts %}
<div class="content-item">
<strong>{{ post.username }}</strong>
<p>{{ post.content }}</p>
<small>Posted on: {{ post.created_at }}</small>
</div>
{% endfor %}
</div>
<footer>
<a href="{{url_for('aboutus')}}">about us</a>
</footer>
</main>
<script src="{{ url_for('static', filename='js/script.js') }}"></script>
</body>
</html>
@@ -4,21 +4,23 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
<link rel="icon" href="{{ url_for('static', filename='icon.png') }}"> <!--添加图标-->
<title>Sign In</title>
</head>
<body>
<main>
<h1>Sign In</h1>
<h1 style="text-align: center;">Sign In</h1>
<form action="/signin" method="post">
<label for="account">Account:</label>
<input type="text" id="account" name="account" required><br>
<label for="password">Password:</label>
<input type="password" id="password" name="password" required><br>
<label for="captcha">Captcha:</label>
<input type="text" id="captcha" name="captcha" required><br>
<input type="text" id="captcha" name="captcha" required>
<img id="captcha-image" src="{{ url_for('captcha') }}" alt="Captcha"><br>
<button type="submit">Sign In</button>
</form>
</main>
<script src="js/validate.js"></script>
<script src="{{ url_for('static', filename='validate.js') }}"></script>
</body>
</html>
@@ -4,25 +4,31 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
<link rel="icon" href="{{ url_for('static', filename='icon.png') }}"> <!--添加图标-->
<title>注册</title>
</head>
<body>
<main>
<h1>注册</h1>
<form action="/signup" method="post">
<h1 style="text-align: center;">Sign up</h1>
<!-- <div id="error-message" style="color: red;"></div> -->
<form action="/signup" method="post" id="signup-form">
<label for="username">用户名:</label>
<input type="text" id="username" name="username" required><br>
<label for="password">密码:</label>
<input type="password" id="password" name="password" required><br>
<label for="confirm-password">确认密码:</label>
<input type="password" id="confirm-password" name="confirm-password" required><br>
<label for="age">年龄:</label>
<input type="number" id="age" name="age" required><br>
<label for="captcha">验证码:</label>
<input type="text" id="captcha" name="captcha" required>
<img id="captcha-image" src="/captcha" alt="Captcha"><br>
<img id="captcha-image" src="{{ url_for('captcha') }}" alt="Captcha"><br>
<button type="submit">注册</button>
</form>
<!-- <footer>
<a href="{{ url_for('') }}">index</a>
</footer> -->
</main>
<script src="js/validate.js"></script>
<script src="js/captcha.js"></script>
<script src="{{ url_for('static', filename='validate.js') }}"></script>
</body>
</html>
+45
View File
@@ -0,0 +1,45 @@
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
<title>User Profile</title>
</head>
<body>
<header>
<nav>
<ul>
<li><a href="{{ url_for('signin') }}">Sign In</a></li>
<li><a href="{{ url_for('signup') }}">Sign Up</a></li>
<li><a href="{{ url_for('dashboard') }}">Dashboard</a></li>
{% if 'user' in session %}
<li><a href="{{ url_for('user') }}">User Profile</a></li>
<li><a href="{{ url_for('logout') }}">Logout</a></li>
{% endif %}
</ul>
</nav>
</header>
<main class="user-profile">
<h1>User Profile</h1>
<div class="user-info">
<p><strong>Username:</strong> {{ user_info.username }}</p>
<p><strong>Age:</strong> {{ user_info.age }}</p>
<p><strong>Registered At:</strong> {{ user_info.created_at }}</p>
</div>
<div class="user-posts">
<h2>Posts</h2>
{% for post in user_posts %}
<div class="content-item">
<p>{{ post.content }}</p>
<small>Posted on: {{ post.created_at }}</small>
<form action="{{ url_for('delete_post', post_id=post.id) }}" method="post" class="delete-form">
<button type="submit" class="delete-button">Delete</button>
</form>
</div>
{% endfor %}
</div>
<a href="{{ url_for('logout') }}" class="logout-button">Logout</a>
</main>
</body>
</html>