{"id":24535,"date":"2024-11-21T08:44:41","date_gmt":"2024-11-21T00:44:41","guid":{"rendered":"https:\/\/fwq.ai\/blog\/24535\/"},"modified":"2024-11-21T08:44:41","modified_gmt":"2024-11-21T00:44:41","slug":"%e6%9e%84%e5%bb%ba%e4%b8%aa%e6%80%a7%e5%8c%96%e7%9a%84%e7%85%a7%e7%89%87%e5%88%86%e4%ba%ab%e5%b9%b3%e5%8f%b0%ef%bc%9awebman%e7%9a%84%e7%85%a7%e7%89%87%e5%ba%94%e7%94%a8%e6%8c%87%e5%8d%97","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/24535\/","title":{"rendered":"\u6784\u5efa\u4e2a\u6027\u5316\u7684\u7167\u7247\u5206\u4eab\u5e73\u53f0\uff1aWebman\u7684\u7167\u7247\u5e94\u7528\u6307\u5357"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/887\/227\/169303915699335.jpg\" class=\"aligncenter\" title=\"\u6784\u5efa\u4e2a\u6027\u5316\u7684\u7167\u7247\u5206\u4eab\u5e73\u53f0\uff1aWebman\u7684\u7167\u7247\u5e94\u7528\u6307\u5357\u63d2\u56fe\" alt=\"\u6784\u5efa\u4e2a\u6027\u5316\u7684\u7167\u7247\u5206\u4eab\u5e73\u53f0\uff1aWebman\u7684\u7167\u7247\u5e94\u7528\u6307\u5357\u63d2\u56fe\" \/><\/p>\n<p>\u6784\u5efa\u4e2a\u6027\u5316\u7684\u7167\u7247\u5206\u4eab\u5e73\u53f0\uff1aWebman\u7684\u7167\u7247\u5e94\u7528\u6307\u5357<\/p>\n<p>\u6458\u8981\uff1a<br \/>\u968f\u7740\u79d1\u6280\u7684\u8fdb\u6b65\u548c\u667a\u80fd\u624b\u673a\u7684\u666e\u53ca\uff0c\u4eba\u4eec\u5bf9\u4e8e\u62cd\u7167\u548c\u7167\u7247\u5206\u4eab\u7684\u9700\u6c42\u4e0d\u65ad\u589e\u957f\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Webman\u6765\u6784\u5efa\u4e00\u4e2a\u4e2a\u6027\u5316\u7684\u7167\u7247\u5206\u4eab\u5e73\u53f0\u3002Webman\u662f\u4e00\u4e2a\u57fa\u4e8ePython\u8bed\u8a00\u7684web\u6846\u67b6\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\u548c\u6613\u4e8e\u4f7f\u7528\u7684API\u63a5\u53e3\u3002\u901a\u8fc7\u672c\u6587\u7684\u6307\u5357\uff0c\u60a8\u5c06\u5b66\u4f1a\u5982\u4f55\u4f7f\u7528Webman\u6765\u6784\u5efa\u4e00\u4e2a\u5177\u6709\u4e2a\u6027\u5316\u529f\u80fd\u7684\u7167\u7247\u5206\u4eab\u5e73\u53f0\uff0c\u5e76\u6dfb\u52a0\u4e00\u4e9b\u5b9e\u7528\u7684\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<ol>\n<li>\n<p>\u5b89\u88c5Webman<br \/>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5Webman\u3002Webman\u7684\u5b89\u88c5\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u4f7f\u7528pip\u547d\u4ee4\u4e00\u884c\u5b89\u88c5\u5373\u53ef\uff1a<\/p>\n<pre>pip install webman<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<li>\n<p>\u521b\u5efa\u9879\u76ee<br \/>\u5728\u5b89\u88c5\u5b8cWebman\u4e4b\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Webman\u7684\u547d\u4ee4\u884c\u5de5\u5177\u6765\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u9879\u76ee\uff1a<\/p>\n<pre>webman create myphotoapp<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u5c06\u521b\u5efa\u4e00\u4e2a\u540d\u4e3amyphotoapp\u7684\u65b0\u9879\u76ee\u76ee\u5f55\uff0c\u5e76\u521b\u5efa\u4e00\u4e2a\u57fa\u672c\u7684\u9879\u76ee\u7ed3\u6784\u3002<\/p>\n<\/li>\n<li>\n<p>\u5b9a\u4e49\u6570\u636e\u5e93\u6a21\u578b<br \/>\u5728myphotoapp\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a\u540d\u4e3amodels.py\u7684\u6587\u4ef6\u3002\u5728\u8fd9\u4e2a\u6587\u4ef6\u4e2d\uff0c\u6211\u4eec\u5c06\u5b9a\u4e49\u7167\u7247\u548c\u7528\u6237\u7684\u6570\u636e\u5e93\u6a21\u578b\uff1a<\/p>\n<pre>from webman import db\n\nclass User(db.Model):\n id = db.Column(db.Integer, primary_key=True)\n username = db.Column(db.String(80), unique=True, nullable=False)\n password = db.Column(db.String(80), nullable=False)\n\nclass Photo(db.Model):\n id = db.Column(db.Integer, primary_key=True)\n title = db.Column(db.String(80), nullable=False)\n filename = db.Column(db.String(80), nullable=False)\n user_id = db.Column(db.Integer, db.ForeignKey('user.id'))\n user = db.relationship('User', backref=db.backref('photos', lazy=True))<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e24\u4e2a\u6a21\u578b\u7c7b\uff1aUser\u548cPhoto\u3002User\u6a21\u578b\u7528\u4e8e\u5b58\u50a8\u7528\u6237\u4fe1\u606f\uff0cPhoto\u6a21\u578b\u7528\u4e8e\u5b58\u50a8\u7167\u7247\u4fe1\u606f\u3002\u6211\u4eec\u4f7f\u7528db.Column\u6765\u5b9a\u4e49\u6a21\u578b\u4e2d\u7684\u5b57\u6bb5\uff0c\u4f7f\u7528db.relationship\u6765\u5b9a\u4e49\u6a21\u578b\u4e4b\u95f4\u7684\u5173\u7cfb\u3002<\/p>\n<\/li>\n<li>\n<p>\u521b\u5efa\u8def\u7531\u548c\u89c6\u56fe<br \/>\u5728myphotoapp\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a\u540d\u4e3aviews.py\u7684\u6587\u4ef6\u3002\u5728\u8fd9\u4e2a\u6587\u4ef6\u4e2d\uff0c\u6211\u4eec\u5c06\u5b9a\u4e49\u7167\u7247\u5e94\u7528\u7684\u8def\u7531\u548c\u89c6\u56fe\u51fd\u6570\uff1a<\/p>\n<pre>from webman import app, db\nfrom webman.auth import login_required\nfrom webman.shortcuts import render_template, redirect, url_for\nfrom .models import User, Photo\n\n@app.route('\/')\n@login_required\ndef index():\n user = User.query.get(session['user_id'])\n photos = Photo.query.filter_by(user_id=user.id).all()\n return render_template('index.html', user=user, photos=photos)\n\n@app.route('\/upload', methods=['GET', 'POST'])\n@login_required\ndef upload():\n if request.method == 'POST':\n     file = request.files['file']\n     filename = secure_filename(file.filename)\n     file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))\n     photo = Photo(title=request.form['title'], filename=filename, user_id=session['user_id'])\n     db.session.add(photo)\n     db.session.commit()\n     return redirect(url_for('index'))\n return render_template('upload.html')<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e24\u4e2a\u8def\u7531\uff1a&#8217;\/&#8217;\u548c&#8217;\/upload&#8217;\u3002&#8217;\/&#8217;\u8def\u7531\u7528\u4e8e\u663e\u793a\u7528\u6237\u7684\u7167\u7247\u5217\u8868\uff0c&#8217;upload&#8217;\u8def\u7531\u7528\u4e8e\u5904\u7406\u7528\u6237\u4e0a\u4f20\u7167\u7247\u7684\u8bf7\u6c42\u3002\u6211\u4eec\u4f7f\u7528@login_required\u88c5\u9970\u5668\u6765\u4fdd\u8bc1\u7528\u6237\u5728\u8bbf\u95ee\u8fd9\u4e9b\u8def\u7531\u65f6\u5df2\u767b\u5f55\u3002<\/p>\n<\/li>\n<li>\n<p>\u521b\u5efa\u6a21\u677f<br \/>\u5728myphotoapp\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a\u540d\u4e3atemplates\u7684\u6587\u4ef6\u5939\uff0c\u5e76\u5728\u5176\u4e2d\u521b\u5efa\u4e24\u4e2aHTML\u6a21\u677f\u6587\u4ef6\uff1aindex.html\u548cupload.html\u3002<br \/>index.html\u7528\u4e8e\u663e\u793a\u7167\u7247\u5217\u8868\uff1a<\/p>\n<pre>{% extends 'base.html' %}\n\n{% block content %}\n&lt;h1&gt;Welcome, {{ user.username }}&lt;\/h1&gt;\n\n&lt;h2&gt;Your Photos&lt;\/h2&gt;\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ul>\n    {% for photo in photos %} <\/p>\n<li>{{ photo.title }}<\/li>\n<p> {% endfor %}\n  <\/ul>\n<p>  {% endblock %}<\/p>\n<p>upload.html\u7528\u4e8e\u663e\u793a\u4e0a\u4f20\u7167\u7247\u7684\u8868\u5355\uff1a<\/p>\n<pre>{% extends 'base.html' %}\n\n{% block content %}\n&lt;h1&gt;Upload a Photo&lt;\/h1&gt;\n\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 {% endblock %}<\/li>\n<li>\n<p>\u8fd0\u884c\u5e94\u7528<br \/>\u5b8c\u6210\u4ee5\u4e0a\u6b65\u9aa4\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Webman\u7684\u547d\u4ee4\u884c\u5de5\u5177\u6765\u8fd0\u884c\u5e94\u7528\uff1a<\/p>\n<pre>webman run<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u5c06\u542f\u52a8\u4e00\u4e2a\u672c\u5730\u670d\u52a1\u5668\uff0c\u5e76\u76d1\u542chttp:\/\/localhost:5000\u3002\u5728\u6d4f\u89c8\u5668\u4e2d\u6253\u5f00\u8fd9\u4e2a\u5730\u5740\uff0c\u5c31\u53ef\u4ee5\u770b\u5230\u6211\u4eec\u521b\u5efa\u7684\u7167\u7247\u5206\u4eab\u5e73\u53f0\u4e86\u3002<\/p>\n<\/li>\n<\/ol>\n<p>\u7ed3\u8bba\uff1a<br \/>\u901a\u8fc7\u672c\u6587\u7684\u6307\u5357\uff0c\u60a8\u5b66\u4f1a\u4e86\u5982\u4f55\u4f7f\u7528Webman\u6765\u6784\u5efa\u4e00\u4e2a\u4e2a\u6027\u5316\u7684\u7167\u7247\u5206\u4eab\u5e73\u53f0\u3002\u6211\u4eec\u901a\u8fc7\u5b9a\u4e49\u6570\u636e\u5e93\u6a21\u578b\u3001\u521b\u5efa\u8def\u7531\u548c\u89c6\u56fe\u3001\u521b\u5efa\u6a21\u677f\uff0c\u5b8c\u6210\u4e86\u4e00\u4e2a\u57fa\u672c\u7684\u7167\u7247\u5206\u4eab\u5e94\u7528\u3002\u60a8\u53ef\u4ee5\u6839\u636e\u81ea\u5df1\u7684\u9700\u6c42\u8fdb\u4e00\u6b65\u6269\u5c55\u8fd9\u4e2a\u5e94\u7528\uff0c\u6bd4\u5982\u6dfb\u52a0\u7528\u6237\u6ce8\u518c\u3001\u8bc4\u8bba\u529f\u80fd\u7b49\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u60a8\u6784\u5efa\u4e2a\u6027\u5316\u7684\u7167\u7247\u5206\u4eab\u5e73\u53f0\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u6784\u5efa\u4e2a\u6027\u5316\u7684\u7167\u7247\u5206\u4eab\u5e73\u53f0\uff1aWebman\u7684\u7167\u7247\u5e94\u7528\u6307\u5357\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6784\u5efa\u4e2a\u6027\u5316\u7684\u7167\u7247\u5206\u4eab\u5e73\u53f0\uff1aWebman\u7684\u7167\u7247\u5e94\u7528\u6307\u5357 \u6458\u8981\uff1a\u968f\u7740\u79d1\u6280\u7684\u8fdb\u6b65\u548c\u667a\u80fd\u624b\u673a\u7684\u666e\u53ca\uff0c\u4eba\u4eec\u5bf9\u4e8e\u62cd\u7167\u548c\u7167\u7247\u5206\u4eab\u7684\u9700\u6c42\u4e0d\u65ad\u589e\u957f\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Webman\u6765\u6784\u5efa\u4e00\u4e2a\u4e2a\u6027\u5316\u7684\u7167\u7247\u5206\u4eab\u5e73\u53f0\u3002Webman\u662f\u4e00\u4e2a\u57fa\u4e8ePython\u8bed\u8a00\u7684web\u6846\u67b6\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\u548c\u6613\u4e8e\u4f7f\u7528\u7684API\u63a5\u53e3\u3002\u901a\u8fc7\u672c\u6587\u7684\u6307\u5357\uff0c\u60a8\u5c06\u5b66\u4f1a\u5982\u4f55\u4f7f\u7528Webman\u6765\u6784\u5efa\u4e00\u4e2a\u5177\u6709\u4e2a\u6027\u5316\u529f\u80fd\u7684\u7167\u7247\u5206\u4eab\u5e73\u53f0\uff0c\u5e76\u6dfb\u52a0\u4e00\u4e9b\u5b9e\u7528\u7684\u4ee3\u7801\u793a\u4f8b\u3002 \u5b89\u88c5Webman\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5Webman\u3002Webman\u7684\u5b89\u88c5\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u4f7f\u7528pip\u547d\u4ee4\u4e00\u884c\u5b89\u88c5\u5373\u53ef\uff1a pip install webman \u767b\u5f55\u540e\u590d\u5236 \u521b\u5efa\u9879\u76ee\u5728\u5b89\u88c5\u5b8cWebman\u4e4b\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Webman\u7684\u547d\u4ee4\u884c\u5de5\u5177\u6765\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u9879\u76ee\uff1a webman create myphotoapp \u767b\u5f55\u540e\u590d\u5236 \u8fd9\u5c06\u521b\u5efa\u4e00\u4e2a\u540d\u4e3amyphotoapp\u7684\u65b0\u9879\u76ee\u76ee\u5f55\uff0c\u5e76\u521b\u5efa\u4e00\u4e2a\u57fa\u672c\u7684\u9879\u76ee\u7ed3\u6784\u3002 \u5b9a\u4e49\u6570\u636e\u5e93\u6a21\u578b\u5728myphotoapp\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a\u540d\u4e3amodels.py\u7684\u6587\u4ef6\u3002\u5728\u8fd9\u4e2a\u6587\u4ef6\u4e2d\uff0c\u6211\u4eec\u5c06\u5b9a\u4e49\u7167\u7247\u548c\u7528\u6237\u7684\u6570\u636e\u5e93\u6a21\u578b\uff1a from webman import db class User(db.Model): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(80), unique=True, nullable=False) password = db.Column(db.String(80), nullable=False) class Photo(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(80), nullable=False) filename = db.Column(db.String(80), nullable=False) user_id = db.Column(db.Integer, db.ForeignKey(&#8216;user.id&#8217;)) user = db.relationship(&#8216;User&#8217;, backref=db.backref(&#8216;photos&#8217;, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-24535","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24535","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=24535"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24535\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=24535"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=24535"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=24535"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}