我正在编写一个 Flask 应用程序,它将存储库名称和电子邮件作为 html 形式的输入参数,并创建一个 github 存储库(如果它尚未存在)。我可以创建存储库并检查它是否已经存在。我的问题是,当存储库不存在时,我想打印另一个静态页面,该页面在浏览器中打印一条消息,显示“GitHub 存储库不存在,正在创建新存储库”。我无法这样做。我的 check_repo 页面是 Flask 中的索引页面。我无法从这里重定向到带有消息的静态页面,然后重定向到将实际创建存储库的 create_repo 页面。
这是我的烧瓶代码:
"""Flask app to create github repo"""
import os
import requests
from flask import Flask, request, render_template, redirect, url_for
from requests.auth import HTTPBasicAuth
app = Flask(__name__)
@app.route('/')
def index():
"""Get html template"""
return render_template('create_repo.html')
@app.route('/check_repo', methods=['POST'])
def repo_exists():
"""Function to check if git repo exists"""
repo_name = request.form.get('Application_Name')
"""Code to check if repo exists"""
response = requests.get(**params)
if response.status_code == 200:
return render_template('repo_exists.html')
return redirect(url_for('create_repo'))
@app.route('/create_repo', methods=['POST'])
def create_repo():
"""Creates a repo"""
application_name = request.form.get('Application_Name')
return render_template('repo_not_found.html', repo_name=application_name)
#"""Create repo code"""
#return redirect(url_for('repo_created')) --> Not sure how to call final message from here after a return above
@app.route('/repo_created')
def repo_created():
"""Prints repo created message"""
return "Repository Created Successfully!"
if __name__ == '__main__':
app.run(debug=True)
下面是我的create_repo.html:
{% extends 'base.html' %}
{% block content %}
<h1>{% block title %} GitHub Repo Automation {% endblock %}</h1>
<form action="/check_repo" method="POST" onsubmit="showLoadingMessage()">
<div class="form-group">
<label for="Application_Name">Application Name</label>
<input type="text" name="Application_Name"
placeholder="GitHub Repo/App Name" class="form-control"
value="{{ request.form['Application_Name'] }}"></input>
</div>
<div class="form-group">
<label for="Owner_Email">Owner Email</label>
<input type="text" name="Owner_Email"
placeholder="Email Address" class="form-control"
value="{{ request.form['Owner_Email'] }}" type="email"></input>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
<div id="loading" style="display: none;">Checking if Git Repository Exists...</div>
<script>function showLoadingMessage(){
document.getElementById('loading').style.display = 'block'
}</script>
<style>
#loading {
color: #007bff;
}
h1 {
border: 2px #eee solid;
color: brown;
text-align: center;
padding: 10px;
}
</style>
</form>
{% endblock %}
您基本上应该为此创建一个 html 页面,然后在端点中呈现它。
@app.route('/check_repo',methods=['POST']) def repo_exists(): """检查 git repo 是否存在的函数""" repo_name = request.form.get('Application_Name') """检查仓库是否存在的代码""" 响应 = requests.get(**params) 如果响应.status_code == 200: 返回 render_template('repo_exists.html') 返回重定向(url_for('repo_does_not_exist')) @app.route('/repo_does_not_exist',methods=['POST']) def repo_does_not_exist(): 返回 render_template('repo_does_not_exist.html')
然后在“repo_does_not_exist.html”中,您应该有您的消息和简单的JavaScript代码,等待一段时间并重定向到“/create_repo”