将Flask pymongo用于学校项目,这是一个共享食谱的网站。 我想返回我在主页上使用的字典,以便站点知道单击时应显示的食谱,但是在终端中,它似乎以我的方式返回(我想要的)作为字典,但实际上返回了字典的字符串,所以我不能使用它。
来自App.py:
@app.route('/', methods=['GET', 'POST'])
def home():
if request.method == 'GET' or request.form.get('search') == '':
recipes = mongo.db.recipe.find()
return render_template('home.html', recipes=recipes)
从主页开始:循环浏览mongodb中的食谱,这样我就可以获取诸如recipe.recipe_name等的值。
{% for recipe in recipes %}
<a href="{{url_for('view_recipe', recipe=recipe)}}"
我将变量返回错误吗?还有另一种方法吗? 所有帮助都非常感谢!
答案 0 :(得分:0)
在获取后端请求时,您必须执行json.loads(your_json)
,该操作可以在后端使用。它将您提到的字典字符串转换为有效的json。