{"id":978980,"date":"2024-12-27T06:42:27","date_gmt":"2024-12-26T22:42:27","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/978980.html"},"modified":"2024-12-27T06:42:29","modified_gmt":"2024-12-26T22:42:29","slug":"%e5%a6%82%e4%bd%95python%e5%88%b6%e4%bd%9c%e7%ad%be%e5%88%b0%e7%b3%bb%e7%bb%9f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/978980.html","title":{"rendered":"\u5982\u4f55python\u5236\u4f5c\u7b7e\u5230\u7cfb\u7edf"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24205020\/d7b287f6-0110-4fcd-b831-d91ff5ed8105.webp\" alt=\"\u5982\u4f55python\u5236\u4f5c\u7b7e\u5230\u7cfb\u7edf\" \/><\/p>\n<p><p> <strong>\u5236\u4f5cPython\u7b7e\u5230\u7cfb\u7edf\u7684\u5173\u952e\u5728\u4e8e\uff1a\u4f7f\u7528\u9002\u5f53\u7684\u6846\u67b6\u6216\u5e93\u3001\u8bbe\u8ba1\u7b80\u5355\u7684\u7528\u6237\u754c\u9762\u3001\u5b9e\u73b0\u6570\u636e\u5b58\u50a8\u529f\u80fd<\/strong>\u3002\u5176\u4e2d\uff0c\u9009\u62e9\u5408\u9002\u7684\u6846\u67b6\u975e\u5e38\u91cd\u8981\uff0cFlask\u6216Django\u662f\u5e38\u7528\u7684Web\u6846\u67b6\uff0c\u5b83\u4eec\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5feb\u901f\u642d\u5efaWeb\u5e94\u7528\u7a0b\u5e8f\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u6df1\u5165\u63a2\u8ba8\u5982\u4f55\u4f7f\u7528Python\u5236\u4f5c\u4e00\u4e2a\u529f\u80fd\u9f50\u5168\u7684\u7b7e\u5230\u7cfb\u7edf\u3002<\/p>\n<\/p>\n<hr>\n<h2><strong>\u4e00\u3001\u9009\u62e9\u5408\u9002\u7684\u6846\u67b6\u548c\u5de5\u5177<\/strong><\/h2>\n<p><p>\u5728\u5f00\u59cb\u5236\u4f5c\u7b7e\u5230\u7cfb\u7edf\u4e4b\u524d\uff0c\u9009\u62e9\u5408\u9002\u7684\u5f00\u53d1\u6846\u67b6\u548c\u5de5\u5177\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002Python\u6709\u8bb8\u591a\u4f18\u79c0\u7684\u6846\u67b6\u548c\u5e93\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5feb\u901f\u5b9e\u73b0\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h2>1\u3001\u4f7f\u7528Flask\u6784\u5efaWeb\u5e94\u7528<\/h2>\n<\/p>\n<p><p>Flask\u662f\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u7684Python Web\u6846\u67b6\uff0c\u9002\u5408\u5c0f\u578b\u9879\u76ee\u548c\u5feb\u901f\u5f00\u53d1\u3002\u5b83\u6613\u4e8e\u5b66\u4e60\u4e14\u7075\u6d3b\u6027\u9ad8\uff0c\u975e\u5e38\u9002\u5408\u7528\u4e8e\u6784\u5efa\u7b80\u5355\u7684\u7b7e\u5230\u7cfb\u7edf\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u5b89\u88c5Flask<\/strong>\uff1a\u53ef\u4ee5\u901a\u8fc7pip\u547d\u4ee4\u8f7b\u677e\u5b89\u88c5<code>Flask<\/code>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install flask<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u521b\u5efa\u57fa\u672c\u5e94\u7528<\/strong>\uff1a\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684Flask\u5e94\u7528\uff0c\u5904\u7406\u57fa\u672c\u7684HTTP\u8bf7\u6c42\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from flask import Flask, request, render_template<\/p>\n<p>app = Flask(__name__)<\/p>\n<p>@app.route(&#39;\/&#39;)<\/p>\n<p>def home():<\/p>\n<p>    return render_template(&#39;index.html&#39;)<\/p>\n<p>if __name__ == &#39;__m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n__&#39;:<\/p>\n<p>    app.run(debug=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<p><h2>2\u3001\u4f7f\u7528Django\u6784\u5efaWeb\u5e94\u7528<\/h2>\n<\/p>\n<p><p>Django\u662f\u4e00\u4e2a\u529f\u80fd\u9f50\u5168\u7684Web\u6846\u67b6\uff0c\u9002\u5408\u5927\u578b\u9879\u76ee\u548c\u590d\u6742\u5e94\u7528\u3002\u5b83\u5185\u7f6e\u4e86\u8bb8\u591a\u529f\u80fd\uff0c\u4f8b\u5982\u7528\u6237\u8eab\u4efd\u9a8c\u8bc1\u3001\u6570\u636e\u5e93\u7ba1\u7406\u7b49\uff0c\u975e\u5e38\u9002\u5408\u7528\u4e8e\u6784\u5efa\u590d\u6742\u7684\u7b7e\u5230\u7cfb\u7edf\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u5b89\u88c5Django<\/strong>\uff1a\u540c\u6837\u53ef\u4ee5\u901a\u8fc7pip\u547d\u4ee4\u5b89\u88c5<code>Django<\/code>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install django<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u521b\u5efaDjango\u9879\u76ee<\/strong>\uff1a\u4f7f\u7528\u547d\u4ee4\u884c\u5de5\u5177\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Django\u9879\u76ee\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">django-admin startproject myproject<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u521b\u5efa\u5e94\u7528\u548c\u89c6\u56fe<\/strong>\uff1a\u5728Django\u9879\u76ee\u4e2d\u521b\u5efa\u5e94\u7528\u548c\u89c6\u56fe\uff0c\u7528\u4e8e\u5904\u7406\u7b7e\u5230\u903b\u8f91\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python manage.py startapp checkin<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u5e94\u7528\u4e2d\u5b9a\u4e49\u89c6\u56fe\u51fd\u6570\u6765\u5904\u7406\u7b7e\u5230\u8bf7\u6c42\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<h2><strong>\u4e8c\u3001\u8bbe\u8ba1\u7528\u6237\u754c\u9762<\/strong><\/h2>\n<p><p>\u4e00\u4e2a\u826f\u597d\u7684\u7528\u6237\u754c\u9762\u53ef\u4ee5\u63d0\u5347\u7528\u6237\u4f53\u9a8c\uff0c\u4f7f\u7b7e\u5230\u7cfb\u7edf\u66f4\u5bb9\u6613\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><h2>1\u3001\u4f7f\u7528HTML\u548cCSS\u8bbe\u8ba1\u7b80\u5355\u7684\u9875\u9762<\/h2>\n<\/p>\n<p><p>\u5728Web\u5e94\u7528\u4e2d\uff0c\u4f7f\u7528HTML\u548cCSS\u8bbe\u8ba1\u7528\u6237\u754c\u9762\u662f\u57fa\u672c\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>HTML\u9875\u9762<\/strong>\uff1a\u521b\u5efa\u7b7e\u5230\u9875\u9762\uff0c\u5305\u542b\u7b7e\u5230\u8868\u5355\u548c\u63d0\u4ea4\u6309\u94ae\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-html\">&lt;!DOCTYPE html&gt;<\/p>\n<p>&lt;html lang=&quot;en&quot;&gt;<\/p>\n<p>&lt;head&gt;<\/p>\n<p>    &lt;meta charset=&quot;UTF-8&quot;&gt;<\/p>\n<p>    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;<\/p>\n<p>    &lt;title&gt;\u7b7e\u5230\u7cfb\u7edf&lt;\/title&gt;<\/p>\n<p>&lt;\/head&gt;<\/p>\n<p>&lt;body&gt;<\/p>\n<p>    &lt;h1&gt;\u7b7e\u5230\u7cfb\u7edf&lt;\/h1&gt;<\/p>\n<p>    &lt;form action=&quot;\/checkin&quot; method=&quot;post&quot;&gt;<\/p>\n<p>        &lt;label for=&quot;username&quot;&gt;\u7528\u6237\u540d\uff1a&lt;\/label&gt;<\/p>\n<p>        &lt;input type=&quot;text&quot; id=&quot;username&quot; name=&quot;username&quot; required&gt;<\/p>\n<p>        &lt;button type=&quot;submit&quot;&gt;\u7b7e\u5230&lt;\/button&gt;<\/p>\n<p>    &lt;\/form&gt;<\/p>\n<p>&lt;\/body&gt;<\/p>\n<p>&lt;\/html&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>CSS\u6837\u5f0f<\/strong>\uff1a\u4e3a\u9875\u9762\u6dfb\u52a0\u6837\u5f0f\uff0c\u4f7f\u5176\u66f4\u7f8e\u89c2\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-css\">body {<\/p>\n<p>    font-family: Arial, sans-serif;<\/p>\n<p>    margin: 0;<\/p>\n<p>    padding: 0;<\/p>\n<p>    display: flex;<\/p>\n<p>    justify-content: center;<\/p>\n<p>    align-items: center;<\/p>\n<p>    height: 100vh;<\/p>\n<p>}<\/p>\n<p>form {<\/p>\n<p>    display: flex;<\/p>\n<p>    flex-direction: column;<\/p>\n<p>}<\/p>\n<p>label, input, button {<\/p>\n<p>    margin: 5px 0;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<p><h2>2\u3001\u96c6\u6210JavaScript\u8fdb\u884c\u52a8\u6001\u4ea4\u4e92<\/h2>\n<\/p>\n<p><p>JavaScript\u53ef\u4ee5\u589e\u5f3a\u9875\u9762\u7684\u4ea4\u4e92\u6027\uff0c\u6bd4\u5982\u5728\u7528\u6237\u8f93\u5165\u65f6\u8fdb\u884c\u6570\u636e\u9a8c\u8bc1\uff0c\u6216\u5728\u63d0\u4ea4\u540e\u663e\u793a\u786e\u8ba4\u4fe1\u606f\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u52a8\u6001\u9a8c\u8bc1<\/strong>\uff1a\u5728\u7528\u6237\u8f93\u5165\u65f6\u9a8c\u8bc1\u4fe1\u606f\u662f\u5426\u7b26\u5408\u8981\u6c42\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">document.getElementById(&#39;username&#39;).addEventListener(&#39;input&#39;, function() {<\/p>\n<p>    var value = this.value;<\/p>\n<p>    if (value.length &lt; 3) {<\/p>\n<p>        this.setCustomValidity(&#39;\u7528\u6237\u540d\u5fc5\u987b\u81f3\u5c11\u5305\u542b3\u4e2a\u5b57\u7b26&#39;);<\/p>\n<p>    } else {<\/p>\n<p>        this.setCustomValidity(&#39;&#39;);<\/p>\n<p>    }<\/p>\n<p>});<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u63d0\u4ea4\u786e\u8ba4<\/strong>\uff1a\u5728\u8868\u5355\u63d0\u4ea4\u65f6\u663e\u793a\u786e\u8ba4\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">document.querySelector(&#39;form&#39;).addEventListener(&#39;submit&#39;, function(e) {<\/p>\n<p>    alert(&#39;\u7b7e\u5230\u6210\u529f\uff01&#39;);<\/p>\n<p>});<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<h2><strong>\u4e09\u3001\u5b9e\u73b0\u6570\u636e\u5b58\u50a8\u529f\u80fd<\/strong><\/h2>\n<p><p>\u7b7e\u5230\u7cfb\u7edf\u9700\u8981\u8bb0\u5f55\u7528\u6237\u7684\u7b7e\u5230\u4fe1\u606f\uff0c\u53ef\u4ee5\u4f7f\u7528\u6570\u636e\u5e93\u6765\u5b58\u50a8\u8fd9\u4e9b\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h2>1\u3001\u9009\u62e9\u6570\u636e\u5e93\u7c7b\u578b<\/h2>\n<\/p>\n<p><p>\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u5e93\u6765\u5b58\u50a8\u7b7e\u5230\u6570\u636e\u3002\u5bf9\u4e8e\u7b80\u5355\u7684\u7b7e\u5230\u7cfb\u7edf\uff0c\u53ef\u4ee5\u9009\u62e9SQLite\uff1b\u5bf9\u4e8e\u9700\u8981\u5904\u7406\u5927\u91cf\u7528\u6237\u548c\u590d\u6742\u67e5\u8be2\u7684\u7cfb\u7edf\uff0c\u53ef\u4ee5\u9009\u62e9MySQL\u6216PostgreSQL\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>SQLite<\/strong>\uff1a\u8f7b\u91cf\u7ea7\u6570\u636e\u5e93\uff0c\u9002\u5408\u5355\u7528\u6237\u5e94\u7528\u6216\u5c0f\u578b\u9879\u76ee\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>MySQL\/PostgreSQL<\/strong>\uff1a\u9002\u5408\u591a\u7528\u6237\u548c\u5927\u89c4\u6a21\u5e94\u7528\uff0c\u652f\u6301\u590d\u6742\u7684\u67e5\u8be2\u548c\u4e8b\u52a1\u5904\u7406\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<p><h2>2\u3001\u914d\u7f6e\u6570\u636e\u5e93\u8fde\u63a5<\/h2>\n<\/p>\n<p><p>\u6839\u636e\u6240\u9009\u62e9\u7684\u6570\u636e\u5e93\uff0c\u914d\u7f6e\u6570\u636e\u5e93\u8fde\u63a5\u5e76\u521b\u5efa\u7b7e\u5230\u6570\u636e\u8868\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>Flask-SQLAlchemy<\/strong>\uff1a\u5982\u679c\u4f7f\u7528Flask\uff0c\u53ef\u4ee5\u4f7f\u7528Flask-SQLAlchemy\u6765\u7ba1\u7406\u6570\u636e\u5e93\u8fde\u63a5\u548c\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from flask_sqlalchemy import SQLAlchemy<\/p>\n<p>app.config[&#39;SQLALCHEMY_DATABASE_URI&#39;] = &#39;sqlite:\/\/\/checkin.db&#39;<\/p>\n<p>db = SQLAlchemy(app)<\/p>\n<p>class Checkin(db.Model):<\/p>\n<p>    id = db.Column(db.Integer, primary_key=True)<\/p>\n<p>    username = db.Column(db.String(80), nullable=False)<\/p>\n<p>    timestamp = db.Column(db.DateTime, default=datetime.utcnow)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>Django ORM<\/strong>\uff1aDjango\u5185\u7f6e\u7684ORM\u53ef\u4ee5\u65b9\u4fbf\u5730\u4e0e\u6570\u636e\u5e93\u8fdb\u884c\u4ea4\u4e92\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from django.db import models<\/p>\n<p>class Checkin(models.Model):<\/p>\n<p>    username = models.CharField(max_length=80)<\/p>\n<p>    timestamp = models.DateTimeField(auto_now_add=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<p><h2>3\u3001\u5b9e\u73b0\u7b7e\u5230\u903b\u8f91<\/h2>\n<\/p>\n<p><p>\u5b9e\u73b0\u540e\u7aef\u903b\u8f91\uff0c\u5904\u7406\u7528\u6237\u7b7e\u5230\u8bf7\u6c42\u5e76\u5c06\u6570\u636e\u5b58\u50a8\u5230\u6570\u636e\u5e93\u4e2d\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>Flask\u5904\u7406\u8bf7\u6c42<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">@app.route(&#39;\/checkin&#39;, methods=[&#39;POST&#39;])<\/p>\n<p>def checkin():<\/p>\n<p>    username = request.form[&#39;username&#39;]<\/p>\n<p>    new_checkin = Checkin(username=username)<\/p>\n<p>    db.session.add(new_checkin)<\/p>\n<p>    db.session.commit()<\/p>\n<p>    return &#39;\u7b7e\u5230\u6210\u529f&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>Django\u5904\u7406\u8bf7\u6c42<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from django.shortcuts import render<\/p>\n<p>from django.http import HttpResponse<\/p>\n<p>from .models import Checkin<\/p>\n<p>def checkin(request):<\/p>\n<p>    if request.method == &#39;POST&#39;:<\/p>\n<p>        username = request.POST[&#39;username&#39;]<\/p>\n<p>        Checkin.objects.create(username=username)<\/p>\n<p>        return HttpResponse(&#39;\u7b7e\u5230\u6210\u529f&#39;)<\/p>\n<p>    return render(request, &#39;checkin.html&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<h2><strong>\u56db\u3001\u6dfb\u52a0\u7528\u6237\u8eab\u4efd\u9a8c\u8bc1<\/strong><\/h2>\n<p><p>\u4e3a\u4e86\u786e\u4fdd\u7b7e\u5230\u6570\u636e\u7684\u771f\u5b9e\u6027\uff0c\u53ef\u4ee5\u6dfb\u52a0\u7528\u6237\u8eab\u4efd\u9a8c\u8bc1\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h2>1\u3001\u4f7f\u7528Flask-Login\u8fdb\u884c\u7528\u6237\u8ba4\u8bc1<\/h2>\n<\/p>\n<p><p>Flask-Login\u662fFlask\u7684\u4e00\u4e2a\u6269\u5c55\u5e93\uff0c\u63d0\u4f9b\u4e86\u7528\u6237\u4f1a\u8bdd\u7ba1\u7406\u548c\u8ba4\u8bc1\u529f\u80fd\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u5b89\u88c5Flask-Login<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install flask-login<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u914d\u7f6e\u7528\u6237\u8ba4\u8bc1<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from flask_login import LoginManager, UserMixin, login_user, login_required, logout_user<\/p>\n<p>login_manager = LoginManager()<\/p>\n<p>login_manager.init_app(app)<\/p>\n<p>class User(UserMixin):<\/p>\n<p>    def __init__(self, id):<\/p>\n<p>        self.id = id<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5b9e\u73b0\u767b\u5f55\u903b\u8f91<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">@app.route(&#39;\/login&#39;, methods=[&#39;GET&#39;, &#39;POST&#39;])<\/p>\n<p>def login():<\/p>\n<p>    if request.method == &#39;POST&#39;:<\/p>\n<p>        username = request.form[&#39;username&#39;]<\/p>\n<p>        user = User(id=username)<\/p>\n<p>        login_user(user)<\/p>\n<p>        return &#39;\u767b\u5f55\u6210\u529f&#39;<\/p>\n<p>    return render_template(&#39;login.html&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<p><h2>2\u3001\u4f7f\u7528Django\u5185\u7f6e\u8ba4\u8bc1\u7cfb\u7edf<\/h2>\n<\/p>\n<p><p>Django\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u7528\u6237\u8ba4\u8bc1\u7cfb\u7edf\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5b9e\u73b0\u7528\u6237\u767b\u5f55\u548c\u6ce8\u518c\u529f\u80fd\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u521b\u5efa\u7528\u6237\u6a21\u578b<\/strong>\uff1a<\/p>\n<\/p>\n<p><p>\u5728Django\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>User<\/code>\u6a21\u578b\u6765\u7ba1\u7406\u7528\u6237\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from django.contrib.auth.models import User<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5b9e\u73b0\u7528\u6237\u6ce8\u518c\u548c\u767b\u5f55\u89c6\u56fe<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from django.contrib.auth import authenticate, login<\/p>\n<p>def user_login(request):<\/p>\n<p>    if request.method == &#39;POST&#39;:<\/p>\n<p>        username = request.POST[&#39;username&#39;]<\/p>\n<p>        password = request.POST[&#39;password&#39;]<\/p>\n<p>        user = authenticate(request, username=username, password=password)<\/p>\n<p>        if user is not None:<\/p>\n<p>            login(request, user)<\/p>\n<p>            return HttpResponse(&#39;\u767b\u5f55\u6210\u529f&#39;)<\/p>\n<p>        else:<\/p>\n<p>            return HttpResponse(&#39;\u767b\u5f55\u5931\u8d25&#39;)<\/p>\n<p>    return render(request, &#39;login.html&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<h2><strong>\u4e94\u3001\u6269\u5c55\u529f\u80fd\u4e0e\u4f18\u5316<\/strong><\/h2>\n<p><p>\u5728\u57fa\u672c\u529f\u80fd\u5b9e\u73b0\u7684\u57fa\u7840\u4e0a\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u4e3a\u7b7e\u5230\u7cfb\u7edf\u6dfb\u52a0\u66f4\u591a\u7684\u529f\u80fd\u548c\u8fdb\u884c\u4f18\u5316\u3002<\/p>\n<\/p>\n<p><h2>1\u3001\u6dfb\u52a0\u7edf\u8ba1\u548c\u62a5\u544a\u529f\u80fd<\/h2>\n<\/p>\n<p><p>\u53ef\u4ee5\u6dfb\u52a0\u7b7e\u5230\u7edf\u8ba1\u548c\u62a5\u544a\u529f\u80fd\uff0c\u751f\u6210\u7528\u6237\u7b7e\u5230\u7684\u7edf\u8ba1\u6570\u636e\u548c\u62a5\u544a\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u7b7e\u5230\u7edf\u8ba1<\/strong>\uff1a\u7edf\u8ba1\u7528\u6237\u7b7e\u5230\u6b21\u6570\uff0c\u751f\u6210\u56fe\u8868\u5c55\u793a\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from matplotlib import pyplot as plt<\/p>\n<p>def generate_report():<\/p>\n<p>    users = Checkin.query.all()<\/p>\n<p>    user_data = {}<\/p>\n<p>    for user in users:<\/p>\n<p>        user_data[user.username] = user_data.get(user.username, 0) + 1<\/p>\n<p>    plt.bar(user_data.keys(), user_data.values())<\/p>\n<p>    plt.xlabel(&#39;\u7528\u6237\u540d&#39;)<\/p>\n<p>    plt.ylabel(&#39;\u7b7e\u5230\u6b21\u6570&#39;)<\/p>\n<p>    plt.title(&#39;\u7b7e\u5230\u7edf\u8ba1&#39;)<\/p>\n<p>    plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u751f\u6210\u62a5\u544a<\/strong>\uff1a\u751f\u6210\u7b7e\u5230\u62a5\u544a\uff0c\u5bfc\u51fa\u4e3aPDF\u6216Excel\u6587\u4ef6\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<p><h2>2\u3001\u4f18\u5316\u6027\u80fd\u548c\u5b89\u5168\u6027<\/h2>\n<\/p>\n<p><p>\u786e\u4fdd\u7b7e\u5230\u7cfb\u7edf\u7684\u6027\u80fd\u548c\u5b89\u5168\u6027\uff0c\u4ee5\u5904\u7406\u66f4\u591a\u7684\u7528\u6237\u548c\u6570\u636e\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u6027\u80fd\u4f18\u5316<\/strong>\uff1a\u4f7f\u7528\u7f13\u5b58\u673a\u5236\u51cf\u5c11\u6570\u636e\u5e93\u67e5\u8be2\u6b21\u6570\uff0c\u63d0\u9ad8\u54cd\u5e94\u901f\u5ea6\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5b89\u5168\u6027<\/strong>\uff1a\u4f7f\u7528HTTPS\u52a0\u5bc6\u4f20\u8f93\u6570\u636e\uff0c\u9632\u6b62\u6570\u636e\u6cc4\u9732\uff1b\u4f7f\u7528CSRF\u4fdd\u62a4\u9632\u6b62\u8de8\u7ad9\u8bf7\u6c42\u4f2a\u9020\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Python\u5236\u4f5c\u4e00\u4e2a\u529f\u80fd\u9f50\u5168\u7684\u7b7e\u5230\u7cfb\u7edf\u3002\u6839\u636e\u9879\u76ee\u9700\u6c42\uff0c\u6211\u4eec\u53ef\u4ee5\u9009\u62e9\u4e0d\u540c\u7684\u6846\u67b6\u548c\u5de5\u5177\uff0c\u8bbe\u8ba1\u7528\u6237\u754c\u9762\uff0c\u5b58\u50a8\u6570\u636e\uff0c\u6dfb\u52a0\u7528\u6237\u8ba4\u8bc1\uff0c\u5e76\u6269\u5c55\u66f4\u591a\u7684\u529f\u80fd\u3002\u65e0\u8bba\u662f\u7528\u4e8e\u5c0f\u578b\u9879\u76ee\u8fd8\u662f\u5927\u578b\u4f01\u4e1a\u5e94\u7528\uff0cPython\u90fd\u80fd\u63d0\u4f9b\u5f3a\u5927\u7684\u652f\u6301\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u5236\u4f5c\u4e00\u4e2a\u7b80\u5355\u7684\u7b7e\u5230\u7cfb\u7edf\uff1f<\/strong><br \/>\u5236\u4f5c\u7b7e\u5230\u7cfb\u7edf\u7684\u7b2c\u4e00\u6b65\u662f\u9009\u62e9\u5408\u9002\u7684\u6846\u67b6\u548c\u5de5\u5177\u3002\u53ef\u4ee5\u4f7f\u7528Flask\u6216Django\u7b49Web\u6846\u67b6\u6765\u5904\u7406\u7528\u6237\u8bf7\u6c42\u4e0e\u6570\u636e\u5e93\u4ea4\u4e92\u3002\u540c\u65f6\uff0cSQLite\u6216MySQL\u53ef\u4ee5\u7528\u4f5c\u6570\u636e\u5b58\u50a8\u3002\u521b\u5efa\u6570\u636e\u5e93\u8868\u683c\u7528\u4e8e\u5b58\u50a8\u7528\u6237\u4fe1\u606f\u548c\u7b7e\u5230\u8bb0\u5f55\uff0c\u7136\u540e\u7f16\u5199\u903b\u8f91\u4ee5\u5141\u8bb8\u7528\u6237\u8fdb\u884c\u7b7e\u5230\u3002<\/p>\n<p><strong>\u7b7e\u5230\u7cfb\u7edf\u7684\u4e3b\u8981\u529f\u80fd\u6709\u54ea\u4e9b\uff1f<\/strong><br \/>\u4e00\u4e2a\u5b8c\u6574\u7684\u7b7e\u5230\u7cfb\u7edf\u901a\u5e38\u5305\u62ec\u7528\u6237\u6ce8\u518c\u3001\u7528\u6237\u767b\u5f55\u3001\u7b7e\u5230\u8bb0\u5f55\u3001\u67e5\u770b\u7b7e\u5230\u5386\u53f2\u7b49\u529f\u80fd\u3002\u7528\u6237\u53ef\u4ee5\u901a\u8fc7\u6ce8\u518c\u8d26\u6237\u6765\u53c2\u4e0e\u7b7e\u5230\uff0c\u7cfb\u7edf\u4f1a\u8bb0\u5f55\u6bcf\u6b21\u7b7e\u5230\u7684\u65f6\u95f4\u548c\u65e5\u671f\u3002\u67e5\u770b\u7b7e\u5230\u5386\u53f2\u7684\u529f\u80fd\u5219\u5141\u8bb8\u7528\u6237\u56de\u987e\u81ea\u5df1\u8fc7\u53bb\u7684\u7b7e\u5230\u8bb0\u5f55\u3002<\/p>\n<p><strong>\u5982\u4f55\u786e\u4fdd\u7b7e\u5230\u7cfb\u7edf\u7684\u5b89\u5168\u6027\uff1f<\/strong><br \/>\u786e\u4fdd\u7b7e\u5230\u7cfb\u7edf\u5b89\u5168\u6027\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u5bc6\u7801\u54c8\u5e0c\u5b58\u50a8\u7528\u6237\u5bc6\u7801\uff0c\u9632\u6b62SQL\u6ce8\u5165\u653b\u51fb\uff0c\u5e76\u5bf9\u7528\u6237\u8f93\u5165\u8fdb\u884c\u9a8c\u8bc1\u3002\u6b64\u5916\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b9e\u65bdHTTPS\u534f\u8bae\u6765\u52a0\u5bc6\u6570\u636e\u4f20\u8f93\uff0c\u4fdd\u62a4\u7528\u6237\u7684\u4e2a\u4eba\u4fe1\u606f\u548c\u7b7e\u5230\u8bb0\u5f55\u4e0d\u88ab\u7a83\u53d6\u3002\u5b9a\u671f\u66f4\u65b0\u7cfb\u7edf\u548c\u4f7f\u7528\u5b89\u5168\u5e93\u4e5f\u662f\u63d0\u5347\u5b89\u5168\u6027\u7684\u91cd\u8981\u6b65\u9aa4\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5236\u4f5cPython\u7b7e\u5230\u7cfb\u7edf\u7684\u5173\u952e\u5728\u4e8e\uff1a\u4f7f\u7528\u9002\u5f53\u7684\u6846\u67b6\u6216\u5e93\u3001\u8bbe\u8ba1\u7b80\u5355\u7684\u7528\u6237\u754c\u9762\u3001\u5b9e\u73b0\u6570\u636e\u5b58\u50a8\u529f\u80fd\u3002\u5176\u4e2d\uff0c\u9009\u62e9\u5408\u9002\u7684 [&hellip;]","protected":false},"author":3,"featured_media":978986,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/978980"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=978980"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/978980\/revisions"}],"predecessor-version":[{"id":978991,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/978980\/revisions\/978991"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/978986"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=978980"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=978980"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=978980"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}