写一个网站门户网站python代码、VUE代码及配套数据库sql
时间: 2024-05-27 19:12:28 浏览: 187
门户网站代码和数据库
抱歉,我是一名AI语言模型,我无法为您编写完整的门户网站。但是,我可以为您提供一些示例代码,以供参考。
Python代码:
# 导入必要的模块
from flask import Flask, render_template, request, redirect, url_for
import pymysql
# 创建应用程序
app = Flask(__name__)
# 定义数据库连接信息
db = pymysql.connect(host='localhost', user='root', password='password', database='portal_website')
# 定义路由
@app.route('/')
def index():
# 查询文章列表
cursor = db.cursor()
cursor.execute('select * from article order by id desc')
articles = cursor.fetchall()
return render_template('index.html', articles=articles)
@app.route('/article/<int:id>')
def article(id):
# 根据文章ID查询文章内容
cursor = db.cursor()
cursor.execute('select * from article where id=%s', (id,))
article = cursor.fetchone()
return render_template('article.html', article=article)
@app.route('/add_article', methods=['GET', 'POST'])
def add_article():
if request.method == 'POST':
# 添加新文章
title = request.form['title']
content = request.form['content']
cursor = db.cursor()
cursor.execute('insert into article (title, content) values (%s, %s)', (title, content))
db.commit()
return redirect(url_for('index'))
else:
return render_template('add_article.html')
# 运行应用程序
if __name__ == '__main__':
app.run()
VUE代码:
<template>
<div>
<h1>文章列表</h1>
<ul>
<li v-for="article in articles" :key="article.id">
<router-link :to="'/article/' + article.id">{{ article.title }}</router-link>
</li>
</ul>
<router-view></router-view>
</div>
</template>
<script>
export default {
data() {
return {
articles: []
}
},
mounted() {
// 获取文章列表数据
fetch('/api/articles')
.then(response => response.json())
.then(data => {
this.articles = data
})
}
}
</script>
数据库SQL:
-- 创建数据库
CREATE DATABASE portal_website;
-- 使用数据库
USE portal_website;
-- 创建文章表
CREATE TABLE article (
id INT PRIMARY KEY AUTO_INCREMENT,
title VARCHAR(100) NOT NULL,
content TEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- 插入示例数据
INSERT INTO article (title, content) VALUES ('Python入门', 'Python是一门易学易用的编程语言。');
INSERT INTO article (title, content) VALUES ('Vue入门', 'Vue是一款流行的JavaScript框架,用于构建Web应用程序。');
阅读全文