Search code examples
pythonhtmlflask

Handling multiple requests in Flask with a static page in between


I am writing a flask app that takes repo name and email as input parameter in html form and creates a github repo, if it already does not exixts. I am able to create repo and check if it already exists. My issue is when repo does not exist, I want to print another static page which prints a message in browser saying "GitHub Repo does not exist, Creating new repo". I am unable to do so. My check_repo page is index page in flask. I am unable redirect from here to static page with message and then create_repo page which will actually create the repo.

Here is my flask code:

"""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)

Below is my 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 %}

Solution

  • You should basically create an html page for that, then render it in an endpoint.

    
        @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('repo_does_not_exist'))
        
        @app.route('/repo_does_not_exist', methods=['POST'])
        def repo_does_not_exist():
            return render_template('repo_does_not_exist.html')
    
    

    Then in 'repo_does_not_exist.html', you should have your message and a simple javascript code to wait for some time and redirect to '/create_repo'