运行Flask时出现404 Not Found错误

时间:2017-06-22 02:04:34

标签: python flask flask-sqlalchemy flask-wtforms

我正在开发一个简单的Flask应用程序,基于这个例子: https://gist.github.com/cuibonobo/8696392#apache

以下是文件夹结构:

flaskapp:
   app:
      __init__.py
      static:
          css:
              main.css
      templates:
          404.html
          base.html
          forms:
              macros.html
          register.html
          users:
              login.html
              profile.html
          register.html
      users:
         __init__py
         constants.py
         decorators.py
         forms.py
         models.py
         views.py
app.db
config.py
run.py
shell.py

run.py:

#!flask/bin/python
from app import app
app.run(debug=True)

shell.py:     #!/ usr / bin / env python     进口口     导入readline     来自pprint import pprint

from flask import *
from app import *

os.environ['PYTHONINSPECT'] = 'True'

views.py:

from flask import Blueprint, request, render_template, flash, g, session, redirect, url_for
from werkzeug import check_password_hash, generate_password_hash

from app import db
from app.users.forms import RegisterForm, LoginForm
from app.users.models import User
from app.users.decorators import requires_login

mod = Blueprint('users', __name__, url_prefix='/users')


@mod.route('/')
@requires_login
def home():
return render_template("users/profile.html", user=g.user)

@mod.before_request
def before_request():
"""
pull user's profile from the database before every request are treated
"""
g.user = None
if 'user_id' in session:
  g.user = User.query.get(session['user_id'])

@mod.route('/login/', methods=['GET', 'POST'])
def login():
  """
  Login form
  """
  form = LoginForm(request.form)
  # make sure data are valid, but doesn't validate password is right
  if form.validate_on_submit():
    user = User.query.filter_by(email=form.email.data).first()
    # we use werzeug to validate user's password
    if user and check_password_hash(user.password, form.password.data):
      # the session can't be modified as it's signed, 
      # it's a safe place to store the user id
      session['user_id'] = user.id
      flash('Welcome %s' % user.name)
      return redirect(url_for('users.home'))
    flash('Wrong email or password', 'error-message')
  return render_template("users/login.html", form=form)

@mod.route('/register/', methods=['GET', 'POST'])
def register():
  """
  Registration Form
 """
 form = RegisterForm(request.form)
  if form.validate_on_submit():
    # create an user instance not yet stored in the database
    user = User(name=form.name.data, email=form.email.data, \
      password=generate_password_hash(form.password.data))
    # Insert the record in our database and commit it
    db.session.add(user)
    db.session.commit()

    # Log the user in, as he now has an id
    session['user_id'] = user.id

    # flash will display a message to the user
    flash('Thanks for registering')
    # redirect user to the 'home' method of the user module.
    return redirect(url_for('users.home'))
  return render_template("users/register.html", form=form)

当我通过以下方式启动localhost时:

python run.py

我收到404 Not Found。

我可能做错了什么?

1 个答案:

答案 0 :(得分:0)

我猜你没有为你网站的根目录定义路线。

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