flask_route錯誤:AttributeError: 'function' object has no attribute 'route'

問題:html

  路由徹底正確,當只有一個名爲home的函數處理這個路由時候,下一個路由處理函數,老是提示沒有這個rotue屬性python

Traceback (most recent call last):
  File "E:/workspace/wei-move/manage.py", line 3, in <module>
    from app import app
  File "E:\workspace\wei-move\app\__init__.py", line 10, in <module>
    from app.home import home as home_blueprint
  File "E:\workspace\wei-move\app\home\__init__.py", line 7, in <module>
    import app.home.views
  File "E:\workspace\wei-move\app\home\views.py", line 16, in <module>
    @home.route('/login/')
AttributeError: 'function' object has no attribute 'route'

問題緣由:flask

  本質是home函數名和@home裝飾器有衝突,當取和裝飾器或者對應的app同樣的名字時候,只能對應一個home函數生效,後面的路由函數都將報錯處理app

# coding:utf8

from flask import render_template as render, redirect, url_for
# render_template 返回模板
# redirect 路由重定向
# url_for 聯合路由重定向,指向對應app的url地址
from . import home

# 首頁
@home.route('/')
def home():
    return render('home/index.html')


# 登陸頁面
@home.route('/login/')
def login():
    return render('home/login.html')

那,如何解決這個問題?函數

  因爲函數名和路由裝飾器名相同引發的錯誤,當把函數名和裝飾路由相同的名字替換成其餘名字,必定要娶和路由裝飾器同樣的名字須要在前面加個下劃線,進行區分url

# 首頁
@home.route('/')
def _home():
    return render('home/index.html')


# 登陸頁面
@home.route('/login/')
def login():
    return render('home/login.html')
相關文章
相關標籤/搜索