python无法将数据推送到mongoDB

时间:2017-06-01 16:32:26

标签: python mongodb flask pymongo

下面的代码采用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 screen ::

的一些消息
 [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加密我的密码,同时在密码字段中插入文本,但它仍然是纯文本。不知道我在这里遗失了什么。

1 个答案:

答案 0 :(得分:0)

好的,我得到了一些解决方法。我尝试将mLab(Web应用程序)与此python代码集成,现在我可以看到名为" users"的新集合以及用户名/密码以Json格式存储在那里。但是为什么代码无法连接到我的mongo安装的localhost。