首页 > 解决方案 > Werkzeug 例程 BuildError:无法为端点“索引”构建 url。您的意思是“静态”吗?

问题描述

我正在开发一个烧瓶应用程序以部署在 heroku 上。持续构建它会抛出相同的错误。甚至我尝试更改 FrontEnd 和 BackEnd 模板的文件夹名称。

应用程序.py

import flask
from flask import Flask,render_template,url_for,request
import pandas as pd 
import pickle
from nltk.stem import WordNetLemmatizer as wnl
import re
# load the model from disk
filename = 'NLP_model2.pkl'
clf = pickle.load(open(filename, 'rb'))
cvv=pickle.load(open('vectorizer_ali.pkl','rb'))

# Defining the function for cleaning
def clean_text1(text):  
    text = str(text)
    words = re.sub(r"(@[A-Za-z0-9]+)|([^0-9A-Za-z \t])|(\w+:\/\/\S+)|^rt|http.+?", " ", text )  
    pattren = r"[\d]"
    words = re.sub(pattren, '', words)
    words = words.lower()
    final_words =  [wnl().lemmatize(word) for word in words.split()]
    final_words = ' '.join(final_words)
    return(final_words)
app = Flask(__name__)

@app.route('/')
def man():
    return render_template('index.html')


@app.route('/predict', methods=['POST'])
def predict():
    data1 = request.form['cust_name']
    data2 = request.form['location']
    data3 = request.form['message']
    text_actual = str(data2) + ' ' + str(data3)
    text_actual = clean_text1(text_actual)
    text_actual = [text_actual]
    bag = cvv.transform(text_actual).toarray()
    
    
    pred = clf.predict(bag)
    return render_template('after.html',data=pred)


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

索引.html

<!DOCTYPE html>
<html lang="en">

<head>
    <!-- Required meta tags-->
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="Colorlib Templates">
    <meta name="author" content="Colorlib">
    <meta name="keywords" content="Colorlib Templates">

    <!-- Title Page-->
    <title>Apply for job by Colorlib</title>

    <!-- Font special for pages-->
    <link href="https://fonts.googleapis.com/css?family=Open+Sans:300,300i,400,400i,600,600i,700,700i,800,800i" rel="stylesheet">

    <!-- Main CSS-->
    <link href="css/main.css" rel="stylesheet" media="all">
</head>

<body>
    
    <div class="page-wrapper bg-dark p-t-100 p-b-50">
        <div class="wrapper wrapper--w900">
            <div class="card card-6">
                <div class="card-heading">
                    <h2 class="title">Customer interest</h2>
                </div>
                <div class="card-body">
                    <form method="POST" action="{{url_for('index')}}">
                        <div class="form-row">
                            <div class="name">Customer Name</div>
                            <div class="value">
                                <input class="input--style-6" type="text" name="cust_name" placeholder="enter your name">
                            </div>
                        </div>
                        <div class="form-row">
                            <div class="name">Location</div>
                            <div class="value">
                                <div class="input-group">
                                    <input class="input--style-6" type="text" name="location" placeholder="enter your city">
                                </div>
                            </div>
                        </div>
                        <div class="form-row">
                            <div class="name">Enter your concern</div>
                            <div class="value">
                                <div class="input-group">
                                    <textarea class="textarea--style-6" name="message" placeholder="enter your text here"></textarea>
                                </div>
                            </div>
                        </div>
                       
                    </form>
                </div>
                <div class="card-footer">
                    <button class="btn btn--radius-2 btn--blue-2" type="submit">Predict!</button>
                </div>
            </div>
        </div>
    </div>

    <!-- Jquery JS-->
    <script src="vendor/jquery/jquery.min.js"></script>


    <!-- Main JS-->
    <script src="js/global.js"></script>

</body><!-- This templates was made by Colorlib (https://colorlib.com) -->

</html>
<!-- end document-->

after.html

<html>
<style>
body {
  background-image: url('ombre.jpg');
  background-repeat: no-repeat;
  background-attachment: fixed;  
  background-size: cover;
}
</style>

    <center>

        <h1>  CUSTOMER'S INTEREST : </h1>
    
    {%if data == 0 %}
    <h1>The Customer is Not-converted</h1>

    {%elif data==1 %}
    <h1>The customer is Converted</h1>

    {% endif %}
        <br><br>
    <a href='/'>go back to home page</a>

    </center>

</body>

</html>

现在这里的目录结构是这样的:

C:\BEPEC Python Material\NLP project\Frontend 这里 Frontend 是主文件夹/子文件夹,其中包含部署的所有相关文件。现在 Frontend 本身有 3 个子文件夹(js、templates、vendor)以及 app.py 和其他 .pkl 文件。 前端文件夹

下面是模板文件夹。然而,前端和后端的运行都与我想要的完全一样 模板文件夹

下面是js

在此处输入图像描述

下面是 vendor 。(供应商只有 1 个子文件夹“jquery”)

在此处输入图像描述

标签: flaskherokunlpbackendweb-deployment

解决方案


Could not build url for endpoint 'index'. Did you mean 'static' instead?

那是指您的模板的这一部分:

<form method="POST" action="{{url_for('index')}}">

url_for函数需要一个参数,它是 Python 代码中由@app.route装饰器装饰的函数的名称。

在您的情况下,我认为您正在寻找该predict功能,该功能似乎可以处理来自表单的发布请求。

<form method="POST" action="{{url_for('predict')}}">

请注意,当您在呈现的页面上查看源代码时,它实际上会呈现到通过该@app.route装饰器提供的 URL 路径:

<form method="POST" action="/predict">

推荐阅读