简体   繁体   中英

AttributeError: type object has no attribute 'as_view' using docker-compose

I am new to Docker and am trying to create a simple form to be built with Docker and displayed through my localhost. When I try to sudo docker-compose up , I get the error AttributeError: type object 'UserForm' has no attribute 'as_view' . I was under the impression that I would be able to see the immediate result on my 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>

I don't know much about Flask, but I use Django and they appear to be similar in this case.

You are trying to render the view using the UserForm , while you should be rendering a view (in your case the index.php), with the form inside a context variable, and then render the form fields inside the html file.

--edit-- some help with views: http://flask.pocoo.org/docs/0.12/tutorial/views/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM