1

I have been trying to add CSS to my webpage using flask but it's not happening. If I run my webpage independently, it shows the CSS effects. But when I run the python code it displays plain HTML form.

This is HTML Code:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-g">
    <title>Forms</title>
    <link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='style.css') }}">
</head>
<body>

    <form action="/view" method="POST" enctype="multipart/form-data">
        <div>
            <p>
                <label for="name">Name
                    <input name="name" type="text" placeholder="Enter Name">
                </label>
            </p>
        </div>

        <div>
            <p>E-mail
                <input name="email" type="email">
            </p>
        </div>

        <div>
            <p>Password
                <input name="pass" type="password">
            </p>
        </div>

        <div>
            <p>
                <input name="file" type="file">
            </p>
        </div>

        <div>
            <p>
                <input type="submit">
            </p>
        </div>
    </form>

</body>
</html>

This is the python code:

from flask import Flask

app = Flask(__name__)

@app.route('/')
def home():
    return render_template("forms.html")

if __name__ == '__main__':
    app.run(host='192.168.1.7', port=int(8000), debug=True)
    print("Server Started successfully")
2
  • Well, "{{ url_for('static', filename='style.css') }}" and "C:\Python\test1\static\css\style.css" are clearly different paths. Why don't you just have one <link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='css/style.css') }}">? Commented May 28, 2020 at 9:24
  • I did that to check if the HTML file is taking CSS independently. Commented May 28, 2020 at 10:48

1 Answer 1

2

There is a slight issue with your code.

Try adding the following as href for stylesheet:

url_for('static', filename='css/style.css')

Argument filename must have the path of file inside static folder.

Sign up to request clarification or add additional context in comments.

1 Comment

I hope you can now accept my answer, since it worked well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.