weixin_33724659 2017-10-10 16:11 采纳率: 0%
浏览 37

获取json的值

Aloha, i am trying to follow this tutorial and here is a Git of it

I was pretty successfull with it but now i am trying to modify this code a little bit :)

from flask import Flask, jsonify, request, render_template

app = Flask(__name__)

Persons = [{'name' : 'Arnold' , 'email' : 'disceps@gmail.com'},
           {'name' : 'Alex Mikro' , 'email' : 'kovalsky@gmail.com'}, 
           {'name' : 'Phost Arnold' , 'email' : 'arn@gmx.de'}]

So, i've created the following value

For now, i would like to get a POST responce from this...So, if a name is equall to the typed name, there should be a responce..

@app.route('/pers', methods=['POST'])
def addOne():
    email = request.form['email']
    name = request.form['name']

if name and email:
    pers = [persons for persons in Persons if persons['name'] == name]
    pers[0]['name'] = request.get_json(force=True).get('name')    
    if name == pers[0]:
        return jsonify({'name' : name})


return jsonify({'error' : 'Missing data!'})

but unfortunately,this doesn't work..

p.s. the code mentioned in the video

def addOne():
email = request.form['email']
name = request.form['name']

if name and email:
    newName = name[::-1]
    return jsonify({'name' : newName})

return jsonify({'error' : 'Missing data!'})

is working without any troubles :) So i think, there is a problem in my IF-Statement...

  • 写回答

1条回答 默认 最新

  • weixin_33676492 2017-10-11 16:09
    关注

    OK. A lot going on in there. Per Davidism, please explain what you are attempting to do.

    Issues:

    • You have a list with multiple dictionaries in your original example.
    • Then you have a form where you are taking in values from two fields('email' and 'name') and assigning them to variable (email and name)
    • Then, if the variables are populated, you are constructing a list (pers) with a list comprehension, where pers will contain any dictionary from the Persons list with a 'name' value that matches the name variable. Why?
    • Then I lose you. The code after that seems to do multiple unnecessary tasks.

    Almost everything under if name and email: makes no sense as a whole. If you explain what your intent is, I think someone can help. As it is, I can't see what you planned to do.

    The example you provide from the video is not doing what you are doing. It is making a new variable newName from the variable name which is derived from the form (name = request.form['name']). Why it is making newName a reversed version of name I don't know, but even with that it is doing something far simpler than you appear to be attempting.

    评论

报告相同问题?

悬赏问题

  • ¥66 关于川崎机器人调速问题
  • ¥15 winFrom界面无法打开
  • ¥15 crossover21 ARM64版本安装软件问题
  • ¥15 mymetaobjecthandler没有进入
  • ¥15 mmo能不能做客户端怪物
  • ¥15 osm下载到arcgis出错
  • ¥15 Dell g15 每次打开eiq portal后3分钟内自动退出
  • ¥200 使用python编写程序,采用socket方式获取网页实时刷新的数据,能定时print()出来就行。
  • ¥15 matlab如何根据图片中的公式绘制e和v的曲线图
  • ¥15 我想用Python(Django)+Vue搭建一个用户登录界面,但是在运行npm run serve时报错了如何解决?