Flask NameError:名称“用户”未定义

时间:2019-06-01 16:23:55

标签: python-3.x session flask jinja2 flask-wtforms

我有一个Flask应用程序: enter image description here

在用户登录后显示的用户仪表板,然后单击导航窗格中的搜索链接,应出现一个搜索页面。当我单击“搜索”时,出现“ NameError:未定义名称'user'”。

跟踪:

NameError
NameError: name 'user' is not defined

Traceback (most recent call last)
File "C:\Users\fbagi\AppData\Roaming\Python\Python37\site-packages\flask\app.py", line 2309, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Users\fbagi\AppData\Roaming\Python\Python37\site-packages\flask\app.py", line 2295, in wsgi_app
response = self.handle_exception(e)
File "C:\Users\fbagi\AppData\Roaming\Python\Python37\site-packages\flask\app.py", line 1741, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\fbagi\AppData\Roaming\Python\Python37\site-packages\flask\_compat.py", line 35, in reraise
raise value
File "C:\Users\fbagi\AppData\Roaming\Python\Python37\site-packages\flask\app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\fbagi\AppData\Roaming\Python\Python37\site-packages\flask\app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Users\fbagi\AppData\Roaming\Python\Python37\site-packages\flask\app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\fbagi\AppData\Roaming\Python\Python37\site-packages\flask\_compat.py", line 35, in reraise
raise value
File "C:\Users\fbagi\AppData\Roaming\Python\Python37\site-packages\flask\app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Users\fbagi\AppData\Roaming\Python\Python37\site-packages\flask\app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "C:\Users\fbagi\Documents\GitHub\[edited]\search\views.py", line 32, in search
return render_template('search.html', user=user)
NameError: name 'user' is not defined
The debugger caught an exception in your WSGI application. You can now look at the traceback which led to the error.
To switch between the interactive traceback and the plaintext one, you can click on the "Traceback" headline. From the text traceback you can also create a paste of it. For code execution mouse-over the frame you want to debug and click on the console icon on the right side.

 You can execute arbitrary Python code in the stack frames and there are some extra helpers available for introspection:

dump() shows all variables in the frame
dump(obj) dumps all that's known about the object

search.html:

{% extends "base.html" %} 

{% block title %}{{ user.username }} - Profile{% endblock %} 

{% block header %}<link href="{{ url_for('static', filename='css/base.css') }}" rel="stylesheet">{% endblock %}

{% block content %} 

{% include "navbar.html" %}

<div class="row">

<div class="col-md-3">
<body>

<form action="{{url_for('search_app.search')}}" method="POST">
    <input type="text" name="SearchForm">
<p>        <input type="submit" name="my-form" value="Send"></p>
</form>
{{ results }}
</body>

{% endblock %}

search / views.py

from flask import Blueprint
from flask import Flask, request, render_template, jsonify, url_for, redirect, request
import operator
from search.forms import SearchForm
from user.models import User

search_app = Blueprint('search_app', __name__)
@search_app.route('/search', methods = ['GET', 'POST'])
def search():
    form = SearchForm()
    return render_template('search.html', user=user)

我尝试通过将 user 变量包含在 views.py 中:

user = User.objects.filter(username=session.get('username')).first()

...但是仍然遇到相同的问题。

在运行时导出User对象时,所有内容都会检出:

 >>> from user.models import User
 >>> User
 <class 'user.models.User'>

我在做什么错?

1 个答案:

答案 0 :(得分:0)

您需要传递ROW_NUMBER()类的有效实例。像这样-

Xamarin.Essential.Geolocation.GetLocationAsync()

对于使用flask进行身份验证,您可以参考以下链接