AttributeError:类型对象使用docker-compose没有属性'as_view'

时间:2018-09-07 22:33:28

标签: python docker

我是Docker的新手,正在尝试创建一个使用Docker构建并通过本地主机显示的简单表单。当我尝试sudo docker-compose up时,出现错误AttributeError: type object 'UserForm' has no attribute 'as_view'。我的印象是,我可以在localhost:5002上看到立即的结果。

-api.py-

# docker test 

from flask import Flask, render_template, flash, request
from flask_restful import Resource, Api
from wtforms import Form, TextField, TextAreaField, validators, StringField, SubmitField

app= Flask(__name__)
api= Api(app)
app.config.from_object(__name__)
app.config['SECRET_KEY'] = '7d441f27d441f27567d441f2b6176a'
@app.route("/", methods=['GET', 'POST'])

class UserForm(Form):
    salutation= TextField('Salutation:', validators= [validators.required()])
    first_name= TextField('First Name:', validators= [validators.required()])
    last_name= TextField('Last Name:', validators= [validators.required()])

def welcome():
    form = UserForm(request.form)
    print(form.errors)
    if request.method == 'POST':
        salutation=request.form['salutation']
        first_name=request.form['first_name']
        last_name=request.form['last_name']
        print(salutation)
        print(first_name)
        print(last_name)

        if form.validate():
            flash('Welcome' + salutation + last_name)
        else:
            flash('Please fill in the required fields.')

api.add_resource(UserForm, '/')

if __name__ == '__main__':
    app.run(host= '0.0.0.0', port=80, debug=True)

-requirements.txt-

Flask==0.12
flask-restful==0.3.5
wtforms==2.2

-docker-compose.yml-

version: '2'
services:
  form-service:
    build: ./form
    volumes:
      - ./form:/usr/src/app
    ports:
      - 5001:80

  website:
    image: php:apache
    volumes:
      - ./website:/var/www/html
    ports:
      - 5002:80
    depends_on:
      - form-service

-Dockerfile-

FROM python:3-onbuild
COPY . /usr/src/app
CMD ["python", "api.py"]

-index.php-

<html>
    <head>
        <title>Docker Test</title>
    </head>
    <body>
        {% with messages = get_flashed_messages(with_categories=true) %}
            {% if messages %}
                <ul>
                    {% for message in messages %}
                        <li>{{ message[1] }}</li>
                    {% endfor %}
                </ul>
            {% endif %}
        {% endwith %}
        <form action="" method="post">
            {{ form.csrf }}

            <div class="input text">
                {{ form.salutation.label }} {{ form.salutation }}
            </div>

            <div class="input text">
                {{ form.first_name.label }} {{ form.first_name }}
            </div>

            <div class="input text">
                {{ form.last_name.label }} {{ form.last_name }}
            </div>

            <div class="input submit">
                <input type="submit" value="Submit" />
            </div>
        </form>
    </body>
</html>

1 个答案:

答案 0 :(得分:0)

我对Flask不太了解,但是我使用Django,在这种情况下它们看起来很相似。

您正在尝试使用 UserForm 呈现视图,而您应该呈现 view (在您的情况下为index.php),并且该表单位于上下文变量,然后呈现html文件中的表单字段。

-编辑- 有关视图的一些帮助:http://flask.pocoo.org/docs/0.12/tutorial/views/