Ishaan
Ishaan

Reputation: 1340

Flask 404 error

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.

Upvotes: 0

Views: 724

Answers (3)

Lingster
Lingster

Reputation: 1087

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')

Upvotes: 1

Mikhail Burshteyn
Mikhail Burshteyn

Reputation: 5012

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.

Upvotes: 1

static const
static const

Reputation: 943

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

Upvotes: 0

Related Questions