Flask:NameError:' app'没有定义?

时间:2016-09-01 21:33:09

标签: python flask

我的Flask应用程序在过去24小时内运行良好,但是我只是将其脱机工作并且我尝试再次启动它并且我收到此错误:

Traceback (most recent call last):
  File "runserver.py", line 1, in <module>
    from app import app
  File "/home/MY NAME/APP FOLDER NAME/app.py", line 15, in <module>
    from Views import *
  File "/home/MY NAME/APP FOLDER NAME/Views.py", line 1, in <module>
    @app.route('/contact', methods=('GET', 'POST'))
NameError: name 'app' is not defined

我正在通过调用python runserver.py

来运行应用程序

runserver.py:

from app import app

app.run(threaded = True, debug=True, host='0.0.0.0')

Views.py :包含我的所有路由,我不会将它们全部发布,因为错误来自此文件中第一次提到app

@app.route('/contact', methods=('GET', 'POST'))
def contact():
    form = ContactForm()

    if request.method == 'POST':
        msg = Message("CENSORED,
            sender='CENSORED',
            recipients=['CENSORED'])
        msg.body = """
            From: %s <%s>,
            %s
            """ % (form.name.data, form.email.data, form.message.data)
        mail.send(msg)
        return "><p><br>Successfully  sent message!</p></body>"
    elif request.method == 'GET':
        return render_template('contact.html', form=form)

app.py:这是我的app.py文件的顶部,我在其中定义app = Flask(__name__)以及导入我的语句。

from flask import Flask, request, render_template, redirect, url_for, send_file
from geopy.geocoders import Bing
from geopy.exc import GeocoderTimedOut
import re
import urllib
from bs4 import BeautifulSoup
from openpyxl import load_workbook
from openpyxl.styles import Style, Font
import os
import pandas as pd
import numpy as np
import datetime
from Helper_File import *
from Lists import *
from Views import *
from flask_mail import Mail, Message
from forms import ContactForm

global today

geolocator = Bing('Censored')

app = Flask(__name__)

编辑:我已在下面的答案中进行了更改建议,但是当我访问该页面时会看到此更改:

  

未找到

     

在服务器上找不到请求的URL。如果您手动输入了URL,请检查拼写,然后重试。

这是我的文件结构:

DHLSoftware.com
|-static
    |-style.css
|-templates
    |- seperate html file for each page template
|-app.py
|-forms.py
|-helper_file.py
|-Lists.py
|-runserver.py
|-Views.py

1 个答案:

答案 0 :(得分:1)

StudentData data = new StudentData(); using (var connection = DatabaseConnectivity.GetBranchConnection()) using (var command = connection.CreateCommand()) { command.CommandText = DatabaseConnectivity.GetIsolationLevelStatement(System.Data.IsolationLevel.ReadCommitted) + @"SELECT" + @"branch_name AS [BranchName]," + @"customerID AS [CustomerID]," + @"firstname AS [FirstName]," + @"lastname AS [LastName]," + @"Grades AS [Grades]" + @"from branchdata"; connection.Open(); ViewData["BranchName"] = data.BranchName; ViewData["CustomerID"] = data.CustomerID; ViewData["FirstName"] = data.FirstName; ViewData["LastName"] = data.LastName; ViewData["Grades"] = data.Grades; } return View("StudentDetails", data); 中,您应该移除app.py。而是在from Views import *中,您需要Views.py

这会将应用程序带入views命名空间,我相信这应该适合你。