Python Flask错误-在服务器上找不到请求的URL。如果您手动输入网址,请检查拼写,然后重试

时间:2018-07-02 19:36:38

标签: python flask

我对Flask较陌生,并且尝试在终端中运行代码时运行该应用程序,但收到以下消息:

404 Not Found 
The requested URL was not found on the server. If you entered the URL 
manually please check your spelling and try again.

这是我的app.py文件:

from flask import Flask
from flask_mongoengine import MongoEngine
from flask_sslify import SSLify
import os


app = Flask(__name__)
app.url_map.strict_slashes = False
app.config.from_object('config')
db = MongoEngine(app)

这是我的views.py文件:

from flask import Flask, request, render_template, flash, redirect, 
session, url_for, g, jsonify, make_response


@app.before_request
def before_request():
    g.user = current_user


@app.errorhandler(404)
def not_found_error(error):
    return render_template('404.html'), 404


@app.errorhandler(500)
def internal_error(error):
    return render_template('500.html'), 500


@app.route('/index', methods=['GET', 'POST'])
def index():
    if not session.get('user_id'):
        return redirect(url_for('login'))
    user = User.objects.get(id=session.get('user_id'))
    return render_template('frontpage.html', user=user)

@app.route('/')             
def root():
    return redirect(url_for('index'))


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

这是我的models.py文件:

from flask_mongoengine.wtf import model_form
from mongoengine import Document, StringField, EmailField, 
ReferenceField, IntField, DateTimeField, \
BooleanField, DictField, ListField, EmbeddedDocument, \
EmbeddedDocumentListField, FloatField, FileField

from flask import request
from hashlib import md5
from app import db

class User(Document):
    email = StringField(required=True, unique=True)
    username = StringField(required=True, max_length=50, unique=True)
    password = StringField(required=True)
    aboutme = StringField()
    photo = FileField()

    created_at = DateTimeField()

    password_reset_token = StringField()

    user_type = StringField()  
    is_admin = BooleanField()
    is_logged_in = BooleanField()

    def is_active(self):

        return True

    def get_id(self):
        return self.email

    def is_authenticated(self):
        return self.authenticated

    def is_anonymous(self):
        return False

这是我的base.html

<!DOCTYPE html>
<html>
  <head>

    <title> Hello Everybody! </title>

    <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter- 
bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" media="screen">
<script src="https://code.jquery.com/jquery-latest.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter- 
bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="{{ url_for('.static', filename='js/moment.min.js') }}"> 
</script>
<script type="text/javascript" 
src="//cdnjs.cloudflare.com/ajax/libs/socket.io/1.3.6/socket.io.min.js"> 
</script>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="/static/css/main.css" rel="stylesheet">

</head>

<body>

<!-- Fixed navbar -->
<nav class="navbar navbar-default navbar-static-top">
  <div class="container">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data- 
toggle="collapse" data-target="#navbar" aria-expanded="false" aria- 
controls="navbar">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand" href="/index"> Hello </a>
    </div>
    <div id="navbar" class="navbar-collapse collapse">
      <ul class="nav navbar-nav">

      </ul>
      <ul class="nav navbar-nav navbar-right">

       {% if user is defined %}

        <li class="dropdown">

        <a href="#" class="dropdown-toggle" data-toggle="dropdown" 
role="button" aria-haspopup="true" aria-expanded="false">{{ user.email 
}} <span class="caret"></span></a>

          <ul class="dropdown-menu">


            <li><a href="frontpage">Front Page</a></li>
            <li><a href="profile">Edit Profile</a></li>

            <li role="separator" class="divider"></li>


            {% if user.is_admin %}
            <li><a href="invite">Invite</a></li>
            {% endif %}
            <li><a href="logout">Logout</a></li>
          </ul>
        </li>
      </ul>
    </div><!--/.nav-collapse -->
  </div>
</nav>

<div class="container">


  <div class="row">
    <div class="span12">
        {% include 'flash.html' %}
      {% block content %}{% endblock %}
    </div>
  </div>
</div>  

</body>
</html>

最后是我的frontpage.html:

{% extends "base.html" %}

{% block content %}

<h6> Welcome to my site! </h6>


{% endblock %}

我们将不胜感激!

0 个答案:

没有答案