【问题标题】:python not able to push data to mongoDBpython无法将数据推送到mongoDB
【发布时间】:2017-06-01 16:32:25
【问题描述】:

以下代码获取 html 注册表单数据并假设将其推送到 mongoDB。

from flask import Flask
from flask import request
from flask import render_template, flash,redirect,url_for,session,logging
from flask_pymongo import PyMongo
from wtforms import Form, StringField, TextAreaField,PasswordField,validators
from passlib.hash import sha256_crypt
import bcrypt

app = Flask(__name__)
app.config['MONGO_NAME'] = 'amitesh_DB'
app.config['MONGO_HOST'] = 'mongodb://localhost:27017'

mongo = PyMongo(app)

@app.route('/')
def index():
    if 'username' in session:
        return 'you are logged in as' + session['username'] # gives the message and the name of the username that is logged in that session.
    return render_template('index.html')

@app.route('/login')
def login():
    #users = mongo.db.users
    #   users.find_one({'name' : request.form['username']})
    return ''

#we are checking if the username that we are registering doesn't exist, if it does, it will return index.html

@app.route('/register', methods=['POST','GET'])
def register():

    if request.method == 'POST':
        users = mongo.db.users #connecting the collection called "users" in mongoDB
        existing_user = users.find_one({'name':request.form['username']})# we are looking for a name in the mongoDB in the collection "users" that is a username that we are registering with.

        if existing_user is None:#if the above request.form doesn't find the username with a name that we enter in the html form, then this "if" will allow the new username to register.
            hashpass = bcrypt.hashpw(request.form['pass'].encode('utf-8'), bcrypt.gensalt())
            users.insert({'name': request.form['username'],'password' : hashpass})
            session['username'] = request.form['username']
            return redirect(url_for('index'))

        return 'username already exist' # means, "existing_users" wasn't none
    return render_template('register1.html')

if __name__ == '__main__':
    app.secret_key = 'mysecret'
    app.run(debug=True)

我有以下问题::

1) 尽管我提到了数据库名称、服务器地址和集合,但是我没有看到 mongo 中的数据,我的 mongo 正在工作,并且我的 python 代码可以与其通信,作为证明,下面是来自 mongo 屏幕的一些消息::

 [thread1] connection accepted from 127.0.0.1:49849 #1 (1 connection now open)
 [conn1] received client metadata from 127.0.0.1:49849 conn1: { driver: { name: "PyMongo", version: "3.4.0" }, os: { type: "Windows", name: "Windows 7", architecture: "AMD64", version: "6.1.7601-SP1" }, platform: "CPython 2.7.12.final.0" }
 [thread1] connection accepted from 127.0.0.1:49850 #2 (2 connections now open)
 [conn2] received client metadata from 127.0.0.1:49850 conn2: { driver: { name: "PyMongo", version: "3.4.0" }, os: { type: "Windows", name: "Windows 7", architecture: "AMD64", version: "6.1.7601-SP1" }, platform: "CPython 2.7.12.final.0" }
 [thread1] connection accepted from 127.0.0.1:49851 #3 (3 connections now open)
 [conn3] received client metadata from 127.0.0.1:49851 conn3: { application: { name: "MongoDB Shell" }, driver: { name: "MongoDB Internal Client", version: "3.4.4" }, os: { type: "Windows", name: "Microsoft Windows 7", architecture: "x86_64", version: "6.1 SP1

2) 在 python 代码中,我使用 bcrypt 加密我的密码,同时在密码字段中插入文本,但它仍然是纯文本。不确定我是否在这里遗漏了什么。

【问题讨论】:

    标签: python mongodb flask pymongo


    【解决方案1】:

    好的,我有一些工作要做。我尝试将 mLab(Web 应用程序)与此 python 代码集成,现在我可以看到名为“users”的新集合,并且用户名/密码以 Json 格式存储在那里。但是为什么代码无法连接到我安装的 mongo localhost。

    【讨论】:

      猜你喜欢
      • 2016-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-09
      • 1970-01-01
      • 2016-03-13
      • 1970-01-01
      相关资源
      最近更新 更多