Forums

Flask-site 404 not found

Hi there

my app is not available when I go ty My-Username.pythonanywhere.com, I have 404 not found.

It's a simple app using Flask.

There's nothing in the error log, I double checked everything that is mentionned in Debugging imports et sys.path and this part is fine. Also there's no app.run() etc...

I checked that all parts use same python version, so I don't know what could be wrong now. The app is working fine on my computer.

Any hint someone ? (beginner here)

ok, it's now working Don't know why though Anyway sorry for the disturbance

not to worry. glad it's working now! don't hesitate to ask again if you need help...

I'm having the exact same problem, do you know what it was that fixed it?

It depends very much on your code. Normally a 404 simply means that you're trying to access a page like http://abaugus.pythonanywhere.com/something and you don't have a view in your Flask code that goes:

@app.route("/something")
def something_handler():
    ...

If you post the URL you're getting the 404 on and as much of your view code as you can (without giving away any secret information, of course) then we might be able to help you narrow it down.

hello i am trying to do the same stuff getting the same error here is my code its just practice code so info is secret from flask import Flask, request, jsonify, make_response from flask_sqlalchemy import SQLAlchemy import uuid from werkzeug.security import generate_password_hash, check_password_hash import jwt import datetime from functools import wraps

app = Flask(name)

app.config['SECRET_KEY'] = 'thereisnosecret' app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://lstapp:legend11@lstapp.mysql.pythonanywhere-services.com/lstapp$testdata'

db = SQLAlchemy(app)

class User(db.Model): id = db.Column(db.Integer, primary_key=True) public_id = db.Column(db.String(50), unique=True) timestmp = db.Column(db.Integer) password = db.Column(db.String(80)) admin = db.Column(db.Boolean) Textfield1 = db.Column(db.String(50)) Textfield2 = db.Column(db.String(50)) Numberfield1 = db.Column(db.Integer) Numberfield2 = db.Column(db.Integer)

def token_required(f): @wraps(f) def decorated(args, *kwargs): token = None

    if 'x-access-token' in request.headers:
        token = request.headers['x-access-token']

    if not token:
        return jsonify({'message' : 'Token is missing!'}), 401

    try:
        data = jwt.decode(token, app.config['SECRET_KEY'])
        current_user = User.query.filter_by(public_id=data['public_id']).first()
    except:
        return jsonify({'message' : 'Token is invalid!'}), 401

    return f(current_user, *args, **kwargs)

return decorated

@app.route('/user', methods=['GET']) @token_required def get_all_users(current_user):

if not current_user.admin:
    return jsonify({'message' : 'Cannot perform that function!'})

users = User.query.all()

output = []

for user in users:
    user_data = {}
    user_data['public_id'] = user.public_id
    user_data['timestmp'] = user.timestmp
    user_data['password'] = user.password
    user_data['admin'] = user.admin
    user_data['TextField1'] = user.Textfield1
    user_data['TextField2'] = user.Textfield2
    user_data['Numberfield1'] = user.Numberfield1
    user_data['Numberfield2'] = user.Numberfield2
    output.append(user_data)

return jsonify({'users' : output})

@app.route('/user/<public_id>', methods=['GET']) @token_required def get_one_user(current_user, public_id):

if not current_user.admin:
    return jsonify({'message' : 'Cannot perform that function!'})

user = User.query.filter_by(public_id=public_id).first()

if not user:
    return jsonify({'message' : 'No user found!'})

user_data = {}
user_data['public_id'] = user.public_id
user_data['timestmp'] = user.timestmp
user_data['password'] = user.password
user_data['admin'] = user.admin
user_data['TextField1'] = user.Textfield1
user_data['TextField2'] = user.Textfield2
user_data['Numberfield1'] = user.Numberfield1
user_data['Numberfield2'] = user.Numberfield2

return jsonify({'user' : user_data})

@app.route('/user', methods=['POST']) @token_required def create_user(current_user): if not current_user.admin: return jsonify({'message' : 'Cannot perform that function!'})

data = request.get_json()

hashed_password = generate_password_hash(data['password'], method='sha256')

new_user = User(public_id=str(uuid.uuid4()), timestmp=datetime.now(), password=hashed_password, admin=False)
db.session.add(new_user)
db.session.commit()

return jsonify({'message' : 'New user created!'})

@app.route('/user/<public_id>', methods=['PUT']) @token_required def promote_user(current_user, public_id): if not current_user.admin: return jsonify({'message' : 'Cannot perform that function!'})

user = User.query.filter_by(public_id=public_id).first()

if not user:
    return jsonify({'message' : 'No user found!'})

user.admin = True
db.session.commit()

return jsonify({'message' : 'The user has been promoted!'})

@app.route('/user/<public_id>', methods=['DELETE']) @token_required def delete_user(current_user, public_id): if not current_user.admin: return jsonify({'message' : 'Cannot perform that function!'})

user = User.query.filter_by(public_id=public_id).first()

if not user:
    return jsonify({'message' : 'No user found!'})

db.session.delete(user)
db.session.commit()

return jsonify({'message' : 'The user has been deleted!'})

@app.route('/login') def login(): auth = request.authorization

if not auth or not auth.username or not auth.password:
    return make_response('Could not verify', 401, {'WWW-Authenticate' : 'Basic realm="Login required!"'})

user = User.query.filter_by(name=auth.username).first()

if not user:
    return make_response('Could not verify', 401, {'WWW-Authenticate' : 'Basic realm="Login required!"'})

if check_password_hash(user.password, auth.password):
    token = jwt.encode({'public_id' : user.public_id, 'exp' : datetime.datetime.utcnow() + datetime.timedelta(minutes=30)}, app.config['SECRET_KEY'])

    return jsonify({'token' : token.decode('UTF-8')})

return make_response('Could not verify', 401, {'WWW-Authenticate' : 'Basic realm="Login required!"'})

if name == 'main': app.run(debug=True)

I don't believe that you can have multiple app.routes to the same route. I think you need to make those single routes that respond to all of the different methods and decide what to do in the function based on the method.

This is my code:

 @app.route('/hello')
def hello_world():
    return render_template('tst1temp.html', title='Welcome', phrase=input("Enter your name: "))

It says 404-Not Found. Anyone know why?? BTW I imported everything correctly. It works when its just

@app.route('/')
def hello_world():
    return render_template('tst1temp.html', title='Welcome', phrase=input("Enter your name: "))

Thx ppls This is the link

Also I tried

@app.route('/')
def hello_world():
    return "hello world"

YET ITS NOT WORKING!!!

http://192.168.1.129:81/ is not a web app on PythonAnywhere.