0

I am making a project using Flask. I am getting an error while running the app. Here is my code for python.

from flask import Flask,render_template

app = Flask(__name__)

app.route('/login')
app.route('/')
def login():
    return render_template('login.html')

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

Here is the HTML page that I want to render.

{% extends 'base.html' %}


{% block title %}
Login | Websitename
{% endblock %}

{% block custom_styles %}

<style>
  div.card, .login{
    justify-content: center;
  }
</style>

{% endblock %}



{% block main_content %}
<div class="d-flex login">
<div class="card d-flex">
  <div class="card-body">

  <h3 class="card-header">Login</h3>
  <input type="text" name="email" placeholder="Email or Username">
  <input type="password" name="password" placeholder="Password">
  <button type="submit" name="button" class="btn btn-block btn-primary">LOGIN</button>

</div>
</div>
</div>

{% endblock %}

Here is the base.html code

<!DOCTYPE html>
<html lang="en"> 
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <link rel="stylesheet" href="{{ url_for('static',filename='bootstrap.min.css') }}">
    <title>{% block title %}{% endblock %}
    </title>
    {% block custom_styles %}{% endblock %}

 </head>
<body>

{% block main_content %}{% endblock %}


<script src="{{ url_for('static',filename='bootstrap.min.js') }}" charset="utf-8"></script>
<script src="{{ url_for('static',filename='jquery.min.js') }}" charset="utf-8"> 
   </script>
    <script src="{{ url_for('static',filename='popper.min.js') }}" charset="utf-8"> 
   </script></body></html>

I am using the PyCharm IDE. I copied the venv folder (only flask and other library files) from another flask project which is also using libraries used by this project. So is this ok to copy the venv folder's library files or I have to install all the libraries again for this project or other error in the code.

Thanks in advance...:)This is the error i am getting.

2
  • "So is this ok to copy the venv folder's library files" – copying virtualenvs is usually a bad idea, it's best to create a new one. Commented Aug 27, 2018 at 18:06
  • can you structure the app files and folders what you have been created. and there is also issue for @ over app Commented Aug 27, 2018 at 18:21

3 Answers 3

1

You're missing @ on your decorators. Your view function must be defined like this:

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

Otherwise the decorator is not applied to anything, so your function is not added as a view for your routes to your flask app.

You can read more about decorators in the PEP where they were introduced.

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

Comments

1

Your decorator looks incorrect:

app.route('/login')
app.route('/')
def login():
    return render_template('login.html')

try adding an @ character:

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

Comments

0
from flask import Flask,render_template

app = Flask(__name__)


@app.route('/')

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

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

you're defining the function for '/' and not login. Let me know if this works

1 Comment

Sorry, this is an error while copying the code.I am defining for both ('/') and ('/login')

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.