在VPS中部署python flask应用程序的路由问题

时间:2020-09-16 22:27:26

标签: python flask routes vps

我有一个使用Python和Flask创建的应用程序,我将其部署在Digital Ocean VPS中,但是当我运行python应用程序时,到模板和静态目录的路由不起作用。

directory

我的.py文件看起来像这样

import os
import configparser

from flask import Flask, jsonify, request, json, render_template, url_for, sessions
from flask_pymongo import PyMongo
from flask_cors import CORS
from flask_bcrypt import Bcrypt
from flask_jwt_extended import JWTManager
from flask_jwt_extended import (create_access_token)

from bson import json_util
from datetime import datetime
from bson.objectid import ObjectId
import json
import chartjs

APP_DIR = os.path.abspath(os.path.dirname(__file__))
STATIC_FOLDER = os.path.join(APP_DIR, 'static')
TEMPLATE_FOLDER = os.path.join(APP_DIR, 'templates')

app = Flask(__name__, static_folder=STATIC_FOLDER,
                template_folder=TEMPLATE_FOLDER,
                )
CORS(app)

app.config['MONGONDB_NAME'] = 'morfee_app'
app.config['MONGO_URI'] = 'mongodb+srv://carvi:ac2502412@sgsss.yv4ar.gcp.mongodb.net/monteria'
app.config['JWT_SECRET_KEY'] = 'secret'

mongo = PyMongo(app)
bcrypt = Bcrypt(app)
jwt = JWTManager(app)


@app.route('/', defaults={'path': ''})
@app.route('/<path:path>')
def serve(path):
    return render_template('login.html')

@app.route('/charts')
def charts():
        if request.method == 'GET':
            eps = mongo.db.morfee.aggregate([
            {
            '$match': {'estado': 'AC', 'regimen': 'subsidiado'}
            },
            {
            '$group': { '_id': '$codigo_EPS', 'Afiliados': {'$sum': 1}}
            },
            {
            '$sort': {'Afiliados': -1}
            }
            ])
            response = json_util.dumps(eps)
            labels, values = [x.get("_id") for x in json.loads(response)], [x.get("Afiliados") for x in json.loads(response)]
            print(labels, values)
        return render_template('chartjs.html', title='EPS Subsidiado', max=140000, labels=labels, values=values)


@app.route('/register', methods=['POST', 'GET'])
def register():
    if request.method == 'POST':
        users = mongo.db.users
        existing_user = users.find_one({'name' : request.form['username']})
        token = request.form['token']

        if token == 'morfee' and existing_user is None:
            pwd = request.form['password']
            hashpass = bcrypt.generate_password_hash(pwd.encode('utf-8'))
            users.insert_one({'name' : request.form['username'], 'email' : request.form['email'], 'password' : hashpass, 'token': request.form['token']})
            try:
                usuario = users.find({'name': request.form['username']},{'name':1,'email':1})
                return render_template('locked.html', usuario = usuario)
            except Exception as e:
                return render_template('500.html')
    else:
        return render_template('signup.html')

@app.route('/login', methods=['POST', 'GET'])
def login():
    if request.method == 'POST':
        users = mongo.db.users
        email = request.form['email']
        password = request.form['password']
        results = ''

        response = users.find_one({'email' : email})

        if response:
            if bcrypt.check_password_hash(response['password'], password):
                access_token = create_access_token(identity = {
                'name' : response['name'],
                'email' : response['email']}
                )
                return render_template('index.html')
            else:
                return render_template('502.html')
        else:
            return render_template('501.html')
        return results
    else:
        return render_template('login.html')

@app.errorhandler(404)
def not_found(error):
    return render_template('404.html'), 404

@app.route('/consulta')
def consulta_get():
    return render_template('consulta-usuarios.html', usuario='')
    
@app.route('/consulta', methods=['POST'])
def consulta_post():
    if request.method == 'POST':
        tipodoc = request.form['tipodoc']
        numdoc = request.form['numdoc']
        ape1 = request.form['ape1']
        ape2 = request.form['ape2']
        nom1 = request.form['nom1']
        nom2 = request.form['nom2']
        fechanto = request.form['fechanto']
        if tipodoc and numdoc:
            usuario = mongo.db.morfee.find_one(
                {'tipo_documento': tipodoc, 'numero_documento': numdoc},{'tipo_documento':1, 'numero_documento':1, 'primer_apellido': 1, 'segundo_apellido': 1, 'primer_nombre': 1, 'segundo_nombre': 1, 'fecha_nacimiento': 1})
            print(usuario)
            return render_template('consulta-usuarios.html', usuario = usuario)
        elif ape1 or ape2 and nom1 or nom2 and fechanto:
            usuario = mongo.db.morfee.find_one(
                {'primer_apellido': ape1, 'segundo_apellido': ape2, 'primer_nombre': nom1, 'segundo_nombre': nom2, 'fecha_nacimiento': fechanto},{'tipo_documento':1, 'numero_documento':1, 'primer_apellido': 1, 'segundo_apellido': 1, 'primer_nombre': 1, 'segundo_nombre': 1, 'fecha_nacimiento': 1})
            print(usuario)
            return render_template('consulta-usuarios.html', usuario = usuario)
        else:
            return render_template('consulta-usuarios.html', usuario = '')
    else:
        return render_template('consulta-usuarios.html', usuario = '')

if __name__ == '__main__':
    app.run(host = '0.0.0.0', port=5000)

我的wsgi文件看起来像这样

from morfeeweb import app

if __name__ == "__main__":
    app.run()

但是模板和静态文件至少在login.html路由中无效。

0 个答案:

没有答案