【问题标题】:Flask NameError: name 'user' is not defined烧瓶名称错误:未定义名称“用户”
【发布时间】:2019-06-01 16:23:55
【问题描述】:

我有一个 Flask 应用程序:

用户登录后显示的用户仪表板,然后单击导航窗格中的搜索链接,应该会出现一个搜索页面。当我点击“搜索”时,我得到一个“NameError: name 'user' is not defined”。

追溯:

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)

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

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

...但仍然遇到同样的问题。

当我在运行时导出用户对象时,一切都检查出来了:

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

我做错了什么?

【问题讨论】:

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


    【解决方案1】:

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

    def search():
        form = SearchForm()
        user = User.objects.filter(username=session.get('username')).first()
        return render_template('search.html', user=user)
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-14
      • 1970-01-01
      • 2022-01-01
      • 1970-01-01
      • 2021-04-02
      • 2015-01-28
      相关资源
      最近更新 更多